Package org.gsm.oneapi.responsebean.mms

Examples of org.gsm.oneapi.responsebean.mms.RetrieveMMSMessageResponse


      
  @see RetrieveMMSMessageResponse
  */
  @SuppressWarnings("deprecation")
  public RetrieveMMSMessageResponse retrieveMessageContent(String registrationId, String messageId, String resFormat) {
    RetrieveMMSMessageResponse response=new RetrieveMMSMessageResponse();
   
    if (registrationId!=null && messageId!=null) {
     
          int responseCode=0;
            String contentType = null;

            try {
        logger.debug("endpoint="+endPoints.getRetrieveMMSMessageEndpoint());
        String endpoint=endPoints.getRetrieveMMSMessageEndpoint().replaceAll("\\{registrationId\\}", URLEncoder.encode(registrationId, "utf-8")).replaceAll("\\{messageId\\}", messageId).replaceAll("\\{resFormat\\}", resFormat);
       
        if (dumpRequestAndResponse) JSONRequest.dumpRequestVariables(endpoint, authorisationHeader, null);
       
        HttpURLConnection con = JSONRequest.setupConnection(endpoint, authorisationHeader);
 
            responseCode=con.getResponseCode();
              contentType = con.getContentType();
              String location=con.getHeaderField("Location");
             
              logger.debug("responseCode="+responseCode+" contentType="+contentType);
             
              response.setHTTPResponseCode(responseCode);
              response.setContentType(contentType);
              response.setLocation(location);
             
              logger.debug("getting input stream");
             
              InputStream input=con.getInputStream();
             
              int boundaryIndex = contentType.indexOf("boundary=");
              String bound=contentType.substring(boundaryIndex + 9).replaceAll("\\\"","");
              System.out.println(bound);
              byte[] boundary = (contentType.substring(boundaryIndex + 9).replaceAll("\\\"","")).getBytes();

              /*
               * Note this API has been marked as deprecated though
               */
              logger.debug("Parsing...");
              MultipartStream multipartStream =  new MultipartStream(input, boundary);
             
              ArrayList<RetrieveMMSMessageResponse.Attachment> attachments=new ArrayList<RetrieveMMSMessageResponse.Attachment>();
             
              boolean nextPart = multipartStream.skipPreamble();
              while(nextPart) {
                String headerSection = multipartStream.readHeaders();
               
                String partContentType=null;
                String partContentName=null;
               
                if (headerSection!=null) {
                  String[] headers=headerSection.split("\n");
                  for (String header:headers) {
                    header=header.trim();
                    if (header.startsWith("Content-Type:")) {
                      String value=header.substring(header.indexOf(":")+1).trim();
                      if (value.indexOf(";")!= -1) {
                        value=value.substring(0, value.indexOf(";"));
                      }
                      partContentType=value;
                    }
                    if (header.startsWith("Content-Disposition:")) {
                      String value=header.substring(header.indexOf(":")+1).trim();
                      if (value.indexOf(";")!= -1) {
                        value=value.substring(0, value.indexOf(";"));
                      }
                      logger.debug("got content disposition="+value);    
                     
                      if (header.indexOf("name=")!=-1) {
                        String name=header.substring(header.indexOf("name=")+5).replaceAll("\\\"", "");
                        logger.debug("Parsed name="+name);
                        partContentName=name;
                      }
                    }
                  }
                }
               
//                System.out.println("Headers: [" + headers+"]");
                ByteArrayOutputStream data = new ByteArrayOutputStream();
                multipartStream.readBodyData(data);
               
                if (partContentType!=null && partContentType.equals("application/json") && partContentName!=null && partContentName.equals("root-fields")) {
                  logger.debug("Have the JSON response part:"+(new String(data.toByteArray())));
                 
                  ObjectMapper mapper=new ObjectMapper();
                  ByteArrayInputStream bais = new ByteArrayInputStream(data.toByteArray());
                  InboundMessageWrapper inboundMessageWrapper=mapper.readValue(bais, InboundMessageWrapper.class);
                  if (inboundMessageWrapper!=null && inboundMessageWrapper.getInboundMessage()!=null) response.setInboundMessage(inboundMessageWrapper.getInboundMessage());                 
                } else {
                  RetrieveMMSMessageResponse.Attachment attachment=new RetrieveMMSMessageResponse.Attachment();
                  attachment.setAttachmentContentType(partContentType);
                  attachment.setAttachmentName(partContentName);
                  attachment.setAttachmentData(data.toByteArray());
                  attachments.add(attachment);
                }

                nextPart = multipartStream.readBoundary();
              }
             
              response.setAttachments(attachments);
             
//              logger.debug("starting conversion to mime message");
//              Properties props = new Properties();
//              Session session = Session.getDefaultInstance(props, null);
//              MimeMessage message = new MimeMessage(session, input);
             
//              logger.debug("getting content");
//              Object content=message.get
//              MimeMultipart content=(MimeMultipart) message.getContent();
//             
//             
//              int sections=content.getCount();
//              logger.debug("Content count="+sections);
             
           
      } catch (Exception e) {
        logger.error("Exception "+e.getMessage()+" "+e.getLocalizedMessage());
        e.printStackTrace();
        response.setHTTPResponseCode(responseCode);
        response.setContentType(contentType);
       
        response.setRequestError(new RequestError(RequestError.SERVICEEXCEPTION, "SVCJAVA", e.getMessage(), e.getClass().getName()));
      }
    }
    return response;
  }
View Full Code Here

TOP

Related Classes of org.gsm.oneapi.responsebean.mms.RetrieveMMSMessageResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.