0
votes

How to create an HTTP adapter to retrieve the file on a web host. I'm confuse because HTTP adapter is used to retrieve json output used for RSS feed. How can i target for files (e.g .jpg).

thanks.

2
The jpg path should be inside the response coming back in json output then you can grab the file.Sami
@Hussam Eddin how to grab the file then?C.Laetitia
Idan's Answer explains exactly how to grab it. I'm following the same steps to get it in my project.Sami
how to use apache cordova in worklight?C.Laetitia
See my Answer, please note we are not required to teach you how to code. you need to try and we can fix for you and assist you.Sami

2 Answers

1
votes

Expanding Idan's answer:

Provide the remote image URL as a parameter to the adapter:

Adapter JS:

function getImage() {
WL.Logger.info("###################  getImage  ######################");

var val = com.company.ProjectName.ImageEncoder.getImage("http://Some-Domain/../.../id.gif");

WL.Logger.info("###################  IMAGE IS  ######################");
WL.Logger.info(val);
WL.Logger.info("#####################################################");
var imageData = {"data":val};
WL.Logger.info(JSON.stringify(imageData));
return imageData;
}

Adapter XML:

Add the procedure to the adapter:

<procedure name="getImage"/>

Add custom Java code to your server:

Java code:

package com.company.ProjectName;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.MalformedInputException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class ImageEncoder {

    public static String getImage(String imageUrl)
            throws MalformedURLException, IOException {

        String imageDataString = "";
        URL url = null;
        int i;
        try {
            url = new URL(imageUrl);
            System.out.println(imageUrl);



            HttpURLConnection connection = null; 
            String protocol = url.getProtocol(); 
            System.out.println(protocol);
          // this is to trust any certificates from the target server 
          if("https".equalsIgnoreCase(protocol)){ 
                      // Create a trust manager that does not validate certificate chains 
              System.out.println("inside If");
                      TrustManager[] trustAllCerts = new TrustManager[]{ 
                          new X509TrustManager() { 
                              public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
                                  return null; 
                              } 
                              public void checkClientTrusted( 
                                  java.security.cert.X509Certificate[] certs, String authType) { 
                              } 
                              public void checkServerTrusted( 
                                  java.security.cert.X509Certificate[] certs, String authType) { 
                              } 
                          } 
                      }; 

                      // Install the all-trusting trust manager 
                SSLContext sc = SSLContext.getInstance("SSL"); 
                sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 

              connection = (HttpsURLConnection)url.openConnection(); 
              System.out.println("connection"+connection.getContentLength()); 
                }else{ 
                  connection=(HttpURLConnection) url.openConnection(); 

          } 


            InputStream input = connection.getInputStream(); 


            byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(input);

            input.close();
            imageDataString = encodeImage(bytes);

            return imageDataString;


        } catch (MalformedInputException malformedInputException) {
            malformedInputException.printStackTrace();
            imageDataString = malformedInputException.toString();
            return ("exception while reading the imag <" + imageDataString + ">");
        } catch (IOException ioException) {
            ioException.printStackTrace();
            imageDataString = ioException.toString();
            return ("exception while reading the imag <" + imageDataString + ">");
        }  catch (NoSuchAlgorithmException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace();
            imageDataString = e.toString();
            return ("exception while reading the imag <" + imageDataString + ">");
    } catch (KeyManagementException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    imageDataString = e.toString();
    return ("exception while reading the imag <" + imageDataString + ">");
} 



    public static String encodeImage(byte[] imageData) {
        // TODO Auto-generated method stub
        org.apache.commons.codec.binary.Base64 base = new org.apache.commons.codec.binary.Base64(
                false);
        return base.encodeToString(imageData);

    }
}

Java code Directory:

enter image description here

NOTE: DON"T TRUST ALL CERTIFICATES. YOU NEED TO ADD YOUR OWN TRUST MANAGER. THIS IS ONLY FOR TESTING

0
votes

You can follow the instructions provided in this blog post.

The steps are as follows:

  1. Provide the remote image URL as a parameter to the adapter
  2. Base64 encode the returned image on the server using a Java utility
  3. Return the encoded base64 string to the application
  4. Base64 decode the string and display the image