Question

when i try to execute this alfresco webscript [http://localhost:8383/alfresco/service/get-order-info] through Advance REST client (google chrome add-on) then it works smoothly but when i try to execute by following code then it gives error at this line JSONObject jsonObject = (JSONObject) new JSONParser().parse(responseString);

public class ComplainMasterDaoImpl implements ComplainMasterDao
{

@Override
public ComplainMaster fetchComplainInfo(String orderId, String user) throws Exception
{
    // TODO Auto-generated method stub
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://localhost:8383/alfresco/service/get-order-info");
    List<NameValuePair> formParams = new ArrayList<NameValuePair>();
    formParams.add(new BasicNameValuePair("orderId", orderId));
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
    httpPost.setEntity(formEntity);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    String responseString = IOUtils.toString(httpEntity.getContent(), "UTF-8");
    JSONObject jsonObject = (JSONObject) new JSONParser().parse(responseString);
    JSONObject resultJson = (JSONObject) jsonObject.get("result");

    System.out.println(resultJson.toString());
    return null;
}
}

and when i debugged it then i got resonseString like Apache Tomcat/6.0.29 - Error report

HTTP Status 401 -

type Status report

message

description This request requires HTTP authentication ().

Apache Tomcat/6.0.29

content of get-order-info.post.desc.xml :

<webscript> 
<shortname>Get Order Information</shortname> 
<description>Used to create complain</description> 
<url>/get-order-info</url> 
<format default="json">  </format> 
<authentication>user</authentication> 
</webscript>
Was it helpful?

Solution

Double check your description file. and check which level of authentication you want to provide while web script development.

In webscript desc.xml file, authentication (optional) is the required level of authentication; valid values are:

  1. none: specifies that no authentication is required at all
  2. guest: specifies that at least guest authentication is required
  3. user: specifies that at least named user authentication is required
  4. admin: specifies that at least a named admin authentication is required

Note: if not specified, the default value is none

Note: The optional runas attribute can be used to force the execution of a web script as a specific user. This can only be specified for web scripts that are stored in the Java Class path.

refer the following link for more details: http://wiki.alfresco.com/wiki/Web_Scripts

Or else if you want to keep your web script for only authenticated users, then you need to pass required authentication details for the user who is accessing the web script from struts. But make sure that the user must exists in alfresco.

So, add following code in your fetchComplainInfo method for basic authentication:

String basic_auth = new String(Base64.encodeBase64((YOUR_USER_NAME+":"+YOUR_PASSWORD).getBytes()));
httpPost.addHeader("Authorization", "Basic " + basic_auth);

So, your method will be like this:

public class ComplainMasterDaoImpl implements ComplainMasterDao
{

@Override
public ComplainMaster fetchComplainInfo(String orderId, String user) throws Exception
{
    // TODO Auto-generated method stub
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://localhost:8383/alfresco/service/get-order-    info");

    String basic_auth = new String(Base64.encodeBase64((YOUR_USER_NAME+":"+YOUR_PASSWORD).getBytes()));
    httpPost.addHeader("Authorization", "Basic " + basic_auth);

    List<NameValuePair> formParams = new ArrayList<NameValuePair>();
    formParams.add(new BasicNameValuePair("orderId", orderId));
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
    httpPost.setEntity(formEntity);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    String responseString = IOUtils.toString(httpEntity.getContent(), "UTF-8");
    JSONObject jsonObject = (JSONObject) new JSONParser().parse(responseString);
    JSONObject resultJson = (JSONObject) jsonObject.get("result");

    System.out.println(resultJson.toString());
    return null;
}
}

OTHER TIPS

Well my guess is that you are authenticated in alfresco in another tab of google chrome and that alfresco picks that up. 401 is an authentication exception and you need to authenticate to alfresco which is not done in your code above. See for example: http://wiki.alfresco.com/wiki/Web_Scripts#Authenticating

The first thing you should do is to check you webscripts description file to find out which authentication method it demands. Since this seems to be a custom webscript in you alfresco installation its hard to tell you where it is to be found. It could be called something like get-order.info.post.desc.xml (strange with a post request to a script named get- BTW) Look at the authentication element.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top