0

I'm building Java servlet that responds to SMS messages. Pure text response works just fine. When I'm trying to include media in response I see image icon (not an actual image I refer by URL) which is indefinitely downloading on phone screen. What is wrong? Below is the code:

private void sendResponse(HttpServletResponse response, String msg){

        String media="<Media>"+mediaURL+"</Media>"; 
        String body="<Body>"+msg+"</Body>";
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Response><Message>"+body + "</Message></Response>";
    try {   
        log.debug("xml="+xml);
        response.setContentType("application/xml;charset=UTF-8");
        response.getWriter().write(xml);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        log.error(ExceptionUtils.getFullStackTrace(e));

    }
}
philnash
  • 70,667
  • 10
  • 60
  • 88
  • media+body fails – Misha Ovsichtcher Dec 02 '18 at 19:27
  • I assume you mean, when you add the media to the xml it fails, because you don't do that in the code above. Also, where does the `mediaUrl` come from as it's not in the method parameters? When you add a `mediaUrl` is that media available publicly on the internet? Is the image particularly large, like a large gif? Is there anything in the Twilio debugger? – philnash Dec 04 '18 at 23:11
  • @philnash Thank for the response Phil. MedilaURL is a string pointing to small public graphic file on the web, less than 10K in size. It is defined as a class variable in the code therefore doesn't show up here. I didn't look at the Twilio debugger, but I tried different URLs and always get the same result described above. I wonder what might be the reason of that? – Misha Ovsichtcher Dec 06 '18 at 03:01
  • Is there something wrong with the image file itself? What is the image format? Can you look at the [debugger](https://www.twilio.com/console/runtime/debugger) and tell me if it says anything about your image. Can you try a different image? Can you share the link to the image so that I can try it out? – philnash Dec 09 '18 at 07:05
  • I guess I found the reason, and it has nothing to do with tag. Ths MMS was not sent because I had data service turned off on my phone. Though MMS is not supposed to use the data plan (it's a legacy pre-smartphone protocol), it still requires data to be turned on. And when you turn it on, MMS service does use it! My data utilization jumps up with every MMS message sent. Phone companies cheat what they do not tell people this. It may affect people with small data plans. – Misha Ovsichtcher Dec 09 '18 at 15:38
  • Ah, glad you found out what was going on. MMS does use data as far as I know, since SMS is a purely text format. This isn't a smart phone issue though, I had feature phones that used MMS but would have done so over GPRS or 2G data. So, receiving an MMS does use data and if you're worried that your customers/users will have low data plans, then you may want to build in affordances for it. – philnash Dec 10 '18 at 05:48
  • Thanks Phil, that's helpful. Now the task is to receive the incoming image file in my Java servlet. Is there a code snippet how to do that in support documentation? I was only able to find C# example. – Misha Ovsichtcher Dec 11 '18 at 00:59
  • [This question might help you out with that](https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java). – philnash Dec 11 '18 at 01:41
  • Thanks Phil, but may be I did not articulate my question correctly. The image that my servlet receives is part of the incoming MMS message sent via Twilio API . I need to be able to extract the image and save it as file. In C# example, the code makes a number of system and library calls https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-download-images-incoming-mms-csharp?code-sample=code-process-mms-media-with-c&code-language=C%23&code-sdk-version=default – Misha Ovsichtcher Dec 12 '18 at 02:20
  • Sure, so when you receive an MMS message the parameters include `NumMedia` which tells you how many images are attached and then `MediaUrl{N}` where `N` is the index starting from 0. You can use those URLs to then dowload the image. I pointed to the other stack overflow question for how to download something in Java. Does that help? – philnash Dec 12 '18 at 03:57
  • @philnash Phil, is it at all possible to download the media file while on trial? The documentation says one need to do basic authentication with trial SID & Token as username & psw vs. live SID & Token; I tried both, and regardless of that and the download method it gives me 403 error for my mediaURL with ?x-amz-security-token=... attached to the end. – Misha Ovsichtcher Dec 17 '18 at 19:19
  • As far as I am aware media files are not stored behind any authentication. You should just be able to get the URL from the incoming request parameters and download it. You definitely don't want to use the trial SID and token. Where are you seeing that documentation? – philnash Dec 17 '18 at 22:31
  • Thanks for the response Phil. I just recently found very useful article [link](https://www.twilio.com/docs/sms/tutorials/how-to-receive-and-download-images-incoming-mms-java), which helped me implement the required functionality. And you're right, no authentication required. – Misha Ovsichtcher Dec 19 '18 at 01:20
  • Oh my goodness, can't believe I didn't find that tutorial myself. Glad you got it sorted! – philnash Dec 19 '18 at 01:24

0 Answers0