Friday, November 6, 2020

Custom Service to Read Json Data from any provided URL

 

Custom Service to Read Json Data from any provided URL



AppConstants.java (class)

 

package com.community.config.core;

 

public class AppConstants {  

      public static final String URL = "https://jsonplaceholder.typicode.com/todos/1";

}

 

ReadJsonService.java (interface)

 

package com.community.config.core;

 

public interface ReadJsonService {

      public String getData();

}

 

ReadJsonDataImpl.java (class)

 

package com.community.config.core;

 

import org.osgi.service.component.annotations.Component;

import static com.community.config.core.AppConstants.URL;

import com.community.config.core.ReadJsonService;

import com.community.config.core.Network;

 

@Component(immediate = true, service = ReadJsonService.class)

public class ReadJsonDataImpl implements ReadJsonService {

 

      @Override

      public String getData() {    

            String response = Network.readJson(URL);       

            return response;

      }

 

}

 

Network.java (class)

 

package com.community.config.core;

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

 

public final class Network {

      private static final String USER_AGENT = "Mozilla/5.0";

      public static String readJson(String url) {

            try {

                  URL requestURL = new URL(url);

                  HttpsURLConnection connection = (HttpsURLConnection) requestURL.openConnection();

                  connection.setRequestMethod("GET");

                  connection.setRequestProperty("User-Agent", USER_AGENT);

                  int responseCode = connection.getResponseCode();

                  if (responseCode == HttpsURLConnection.HTTP_OK) {

                        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                        String inputLine;

                        StringBuffer response = new StringBuffer();

                        while ((inputLine = in.readLine()) != null) {

                              response.append(inputLine);

                        }

                        in.close();

                        return response.toString();

                  }

            } catch (MalformedURLException e) {

                  e.printStackTrace();

            } catch (IOException e) {

                  e.printStackTrace();

            }

            return "";

      }

}

 

 

 

Change the code in HelloWorld component’s .html file

Helloworld.html

<pre data-sly-use.jsondata="com.community.config.core.models.HelloWorldModel">

Json Data is:

${jsondata.message}

</pre>

 

Make Changes in HelloWorldModel.java to pass the value in OSGI Bundle

HelloWorldModel (class)

package com.community.config.core.models;

 

import javax.annotation.PostConstruct;

import javax.inject.Inject;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.Model;

import com.community.config.core.ReadJsonService;

 

@Model(adaptables=Resource.class)

public class HelloWorldModel {

     

    @Inject

    ReadJsonService rj;

   

    private String message;

 

    @PostConstruct

    protected void init() {         

        message = rj.getData()+ "\n";

    }

 

    public String getMessage() {

        return message;

    }

}


By this we can hit any URL and get the Json data on Page


No comments:

Post a Comment