cancel
Showing results for 
Search instead for 
Did you mean: 

call Webscript from java

luisg
Champ in-the-making
Champ in-the-making
Hi all,

I'm developing a client / server aplication using GWT(java) linked a postgresql database with hibernate.

I need to do the authentication using Alfresco so the users registered in Alfresco will login in my application.

So do I need to call the Alfresco login webscript (that returns a ticket) in my server part or I just need to do something like as represented here: http://wiki.alfresco.com/wiki/Web_Service_Samples_for_Java ? I'm a little confused…

Can anyone tell me how can I do that, with an example?

Thanks

Luis
4 REPLIES 4

luisg
Champ in-the-making
Champ in-the-making
Hi again…

I did solved this question about login… I added the jars of alfresco and implemented the following method in my server part, that return the ticket:

protected String startSession(String user, String pass){
      String result = "";

        try{
       //setSessionProperties();
       System.out.println("Connecting to: " + ALFRESCOURL);
       WebServiceFactory.setEndpointAddress(ALFRESCOURL+"/api");
       AuthenticationUtils.startSession(user, pass);
         result = AuthenticationUtils.getTicket();
      }
      catch(Exception e)
      {
            System.err.println("Can not initiate session with Alfresco server.");
            e.printStackTrace();
             result = "failed";
       }
        return result;
    }

But now I need to get some information about that user: his/her username, firstName, lastName and the groups his/her is into. I have a webscript that give me that information, but how can I call and get the information from that web script?

Any of you can help me?

sylvain78
Champ in-the-making
Champ in-the-making
Hi,

There are 2 different implementations of the web services in Alfresco (besides the CMIS bindings): SOAP and REST.

SOAP is the "traditional" way, using the WebServiceFactory and AuthenticationUtils classes provided with the web service client.

On the other hand, webscripts can be accessed via the RESTful API using HTTP GET, POST, DELETE and PUT command.

Here is an example I am using in a GWT project:


// Webscript URL      
String url = "http://localhost:8080/alfresco/wcservice/controlled-metadata/subjects?format=json";

// Build http request
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

// Prepare callback methods for error and success
RequestCallback callback = new RequestCallback() {
    public void onError(Request request, Throwable exception) {
        Window.alert("Error: " + exception.getMessage());
    }

    public void onResponseReceived(Request request, Response response) {
        if (200 == response.getStatusCode()) {

       // ANY CUSTOM method to parse JSON response
       parseResponse(response.getText());

   } else {
            Window.alert("Error: Couldn't retrieve JSON (" + response.getStatusText() + ")");
        }
    }
};

// SEND Request to server and catch any errors.
try {
    builder.sendRequest(null, callback);
} catch (RequestException e) {
    Window.alert("Error: Couldn't retrieve JSON (" + e.getMessage() + ")");
}

In other words, you build and send an HTTP request and handle the response (in my case, the response is a JSON string, but it could be XML or HTML).

Not sure about the authentication though, my users are already authenticated when they call the webscript (which is why I am using http://localhost:8080/alfresco/WCSERVICE instead of http://localhost:8080/alfresco/SERVICE, which is for Basic HTTP authentication).

Hope this helps…;-)

Sylvain

sylvain78
Champ in-the-making
Champ in-the-making
Here is an example of what I did to authenticate to my webscript using Basic HTTP authentication:

Have a look at the submitGetRequest method.


private static final String GET = "GET";

private String webscriptUrl = "http://localhost:8080/alfresco/service/controlled-metadata/subjects?format=xml";
private String username;
private String password;

public void getStuffFromXML() throws Exception {

   // Build request url
   URL url = new URL(webscriptUrl);

   // Get response
   InputStream inputStream = submitGetRequest(url, username, password);
   
   DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
   DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
   Document doc = docBuilder.parse(inputStream);

   // Normalize text representation
   doc.getDocumentElement().normalize();

   // Parse XML response
   doSomethingUseful();
}

/**
* Submit an HTTP GET request to the specified url
*/
private InputStream submitGetRequest(URL url, String username, String password) throws IOException {
   
   // openConnection() doesn't actually open the connection,
   // just gives you a URLConnection.  connect() will open the connection.
   HttpURLConnection connection = (HttpURLConnection)url.openConnection();
   connection.setRequestMethod(GET);
   
   // Write auth header
   BASE64Encoder encoder = new BASE64Encoder();
   String encodedCredential = encoder.encode( (username + ":" + password).getBytes() );
   connection.setRequestProperty("Authorization", "BASIC " + encodedCredential);
   
   // Do request
   connection.connect();
   
   return connection.getInputStream();
}

Sylvain

relax
Champ in-the-making
Champ in-the-making
Hello,
is there a simple java main that invoke a web script?

For example for a myspaces web script

http://localhost:8080/alfresco/wcservice/ui/myspaces?f=0&p=/Company%20Home

Thank you.