cancel
Showing results for 
Search instead for 
Did you mean: 

Begin with WebScript to integrate an other app

antonio_lazaro
Champ in-the-making
Champ in-the-making
Hi guys,

Every time that I asked  about how can I integrate my app to Alfresco, the answers are webscripts and the WIKI Page, but I think it very complicated.
Can I use just Java to integrate?How can I make calls from my app to Alfresco directly?Someone could give examples with codes?The examples I think very superficial.
I didn't understand how can I access Alfresco from my app.
I'm using struts 2.
11 REPLIES 11

pmonks
Star Contributor
Star Contributor
While it is technically possible to embed Alfresco within another Java application, doing so faces some practical challenges (in particular the dependencies Alfresco has on specific versions of numerous 3rd party libraries, which your application, being in the same classloader, will be forced to use).  You can look at the "SDK FirstFoundationClient" example to see how this is done, but I would urge caution about choosing this path.

As has been mentioned elsewhere, REST (Web Scripts) is likely to be a better way to integrate an external application with Alfresco, the main reasons being:
  • REST (Web Scripts) is based on HTTP, so the technology that the external application is implemented in is irrelevant (provided it supports HTTP, of course!   :winkSmiley Happy

  • The Web Script framework lets you roll your own APIs, thereby allowing you to fine tune the remote APIs you expose to the external application.  For performance, this is absolutely critical, and the other remote APIs supported by Alfresco (SOAP, JCR-RMI, and, to a lesser extent, the AVM Remote API) cannot come close to Web Scripts in this regard.
If you're finding the Web Script technology difficult to get started with, try taking a look at some of the examples at http://wiki.alfresco.com/wiki/Web_Scripts_Examples.  Install them, execute them manually from a browser, then think how you might invoke them from a Java program (hint: java.net.URLConnection and http://hc.apache.org/httpclient-3.x/ are your friends!).

Cheers,
Peter

antonio_lazaro
Champ in-the-making
Champ in-the-making
I'd like to use Alfresco as server repository.
My app is going to use just repository service as create category,delete category, put and remove files in categorys and search files.
Just it.
I've heard about Alfresco and it WebScript's power but I think a little complex.

hink how you might invoke them from a Java program
Can I using struts 2, put calls to Alfresco and receive/send messages from/to Alfresco?

lucbard
Champ in-the-making
Champ in-the-making
Hi Antonio,

I am facing the same challenge and the only answers I've got so far is : "yes just include it in you jsp and voilà!"

But no one gave me an actual example. How can an external Struts Action, Servlet,  JSP or JSF page can communicate with the repository.

Questions that remain unanswered:

1- Did Alfresco develop a Tag Lib to include in OUTSIDE applications?
OR
2-Should I just, from a servlet, instantiate a HTTP client class of some sort to call the repository resident Webscript?
  2.1 If so, what if the Webscript auth method is set to web client?

What do we do? I don't want my end user to see anything about the Alfresco client, just the content I am querying.

Thank you all,
Luc

pmonks
Star Contributor
Star Contributor
You need to use an HTTP client library to invoke the Web Scripts - if your client application is written in Java (regardless of which Java MVC framework it might be using), your choices include java.net.URLConnection (included in the JDK, but not a particularly powerful implementation) and the Jakarta Commons HTTP Client (http://hc.apache.org/httpclient-3.x/).  I personally prefer the latter (Commons HTTP Client) since (amongst other things) it supports pre-emptive HTTP Basic Authentication (the most performant way to invoke authenticated Web Scripts).

Cheers,
Peter

theorbix
Confirmed Champ
Confirmed Champ
I'm facing the same need of Antonio and Lucbard: I would like to use Web Scripts to interact with the Alfresco "engine" from a custom web application running on Tomcat.

I was thinking to write my application using JSP pages, but what is the correct way  to call the HTTP URLs of Alfresco's Web Scripts from a JSP?

Pmonks was suggesting to use java.net.URLConnection or Jakarta Commons HTTP Client, but this looks a technique that can be used for server-side web applications developed as Java servlets.

If possible, I would like to avoid writing servlets, and this is why I was thinking to use JSPs…. but is this possible?

And can someone post a very simple "hello world" JSP page that shows how to call one of the standard Alfresco Web Scripts, retrieve the results and send them back to the browser?

I know the question may look a bit ridiculous for skilled Alfresco developers, but I'm a newbie and I need some directions…

lucbard
Champ in-the-making
Champ in-the-making
Hey TheOrbix,

I hear you man. I am still looking for a solution. I know I can use an HTTP Client class to access a WebScript to a remote Alfresco Server.

This technique seams a bit primitive and is not what I call "Integration" or Interoperability.
What is needed is a JSP taglib or JSF component to easily call a webscript.

You want to know what else is frustrating: goto this link:
http://wiki.alfresco.com/wiki/Web_Script_Runtimes

I really like Alfresco and I know it is the solution for me. But the documentation is incomplete and not precise enough.

I will ask the question again.

How can I(we) use the JSF tag describe in Web_Script_Runtimes? Is it only to be used in embedded Alfresco?


Thanks again,
Luc Bard

theorbix
Confirmed Champ
Confirmed Champ
Luc, I see what you mean.

I had already seen the wiki page you mentioned: yes, unfortunately there isn't a JSP tag library, and I'm not too sure that there will be one in the future (the Alfresco R&D team seems to be more and more focused on the Web Scripts API).

Anyhow, there is a way to embed Web Script calls in a JSP page, and it's quite easy. See this sample JSP page:

<%@ page import="org.apache.commons.httpclient.HttpClient" %>
<%@ page import="org.apache.commons.httpclient.UsernamePasswordCredentials" %>
<%@ page import="org.apache.commons.httpclient.auth.AuthScope" %>
<%@ page import="org.apache.commons.httpclient.methods.GetMethod" %>
<html>
   <title>Sample Web Script call from JSP</title>
<body>
<h1>Search results </h1>
<hr/>   

<%

   String resultString = "";

   String fld_title= request.getParameter("fld_title");

   if (fld_title == "") {
       resultString = "Enter a search parameter…";
   } else {
     
          resultString = "";
     
          HttpClient client = new HttpClient();
     
          client.getState().setCredentials(
              new AuthScope("localhost", 8080, "Alfresco"),
              new UsernamePasswordCredentials("demo", "demo")
          );
     
     
         GetMethod get = new GetMethod("http://localhost:8080/alfresco/s/api/search/keyword.html?q=@cm%5C%5C%5C:title%5C:%22" + fld_title + "%22");
     
          get.setDoAuthentication( true );
     
          try {
              int status = client.executeMethod( get );
              resultString = get.getResponseBodyAsString();
          } finally {
              get.releaseConnection();
          }
   }
  
%>   

<%=resultString%>
   
</body>

I got this example thanks to a couple of excellent folks at Alfresco (thanks David and Luis…), maybe it will also help you.

pmonks
Star Contributor
Star Contributor
FWIW now that you have the basic Java code working, it shouldn't be hard to wrap that into a taglib that you can use from your JSPs.  And of course if you do that it'd be great to contribute it back here!  :wink:

Cheers,
Peter

theorbix
Confirmed Champ
Confirmed Champ
@pmonks:
Well, I never wrote a wrapper capable of creating a JSP tag library so I don't know where to start.
So I'll leave this task to someone that is smarter than me with Java… Smiley Happy

In the meanwhile I'm continuing my experiments with the basic JSP code I've attached yesterday. There is a problem that I haven't solved yet and maybe you have some ideas: how to implement result set pagination.

My JSP application will be a very simple search client that will allow users to enter one or more search fields (title, author…), execute the search by calling a Web Script and display the list of documents resulting from the search in a paginated way…

To execute the search I was thinking to use a call to the default api/search Web Script:

GetMethod get = new GetMethod("http://localhost:8080/alfresco/s/api/search/keyword.html?q=@cm%5C%5C%5C:title%5C:%22" + fld_title + "%22");
     

The problem is that this Web Script automatically returns the results in a paginated way,  with links to first/last/next pages at the bottom of the result set.  So far so good, but as soon as I click on the "next" link to display the next page in the result set, I get a popup in the browser asking to authenticate to Alfresco… I need to understand what's wrong in what I'm doing.