How to read XF as JSON programmatically?

nagarajuk207258

23-06-2020

I have created an XF and generated the Json by appending 'model.json' to the URL. Now, I want to get the same Json when I hit the same URL in servlet. I am getting Error#400 (bad Request) when I use below piece of code. Do I need to pass any authentication to get HTTP connection. If so, what kind of authentication I should you and how to get the authentication params?

 

HttpURLConnection urlConnection = createHttpConnection(PAGE_URL,null); //Passing Authentication Key as null. See createHttpConnection method below.

 

String query = "";

urlConnection.setRequestProperty("Content-Length", Integer.toString(query.length()));
urlConnection.getOutputStream().write(query.getBytes("UTF8"));

int status = urlConnection.getResponseCode(); //getting the status code as 400

.

.

.

.

 

private static HttpURLConnection createHttpConnection(String urlString, String authKey)
throws IOException, ParseException {
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);

urlConnection.setRequestProperty("Authorization", authKey);
urlConnection.setRequestProperty("Content-Type", "application/json");

urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
LOGGER.info("urlconnection " +urlConnection);
return urlConnection;
}

 

Thanks,

Nagaraju.

AEM 6.5 Experience Fragments

Accepted Solutions (1)

Accepted Solutions (1)

Arun_Patidar

MVP

23-06-2020

It looks like your request is not formed correctly. 

Are you trying this on Author? If yes then you need credentials otherwise not.

 

If you can access XF as JSON from browser then you should be able to access in servlet as well. It is just another http request.

 

I would suggest trying this on publish server. Sample request can be like below (This is an example of use JS-API but you can get logic from here and easily convert in java servlet )

 

var text;
var xfpath = "/content/exp......./" // path to XF, make sure you escape / if needed
    var curReq = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+xfpath+".model.json";
    log.info(curReq);
    var method = new Packages.org.apache.commons.httpclient.methods.GetMethod(curReq);
    var client = new Packages.org.apache.commons.httpclient.HttpClient();
    /* Author specific 
var creds = new Packages.org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin");
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, creds); 
*/
    client.executeMethod(method);
    var responseBody = method.getResponseBodyAsStream();
    log.info(new String(responseBody));

 

Answers (2)

Answers (2)

Vaibhavi

MVP

23-06-2020

Hi @nagarajuk207258 

 

I had similar requirement and faced similar issue where you can extract the json in browser with a selector but unable to get the same outcome via servlet. 

 

Issue is because XF is not exposed with json and by default it supports html . You can refer to the article which is shared by @Himanshu_Singhal as well on the same. 

Solution:

I have created custom API where servlet will pull the required information and give back result in json format.

In these scenarios I would recommend to use content fragments rather than experience fragments where exposing as json is by default available. 

 

In order to fix your issue with XF you can go with custom solution similar like I mentioned above.