In Part One we covered the default installation of Alfresco and in Part Two we covered a JBoss installation. Now we’re going to look at how you would handle linking up your own web application with Alfresco web client.
The structure of the Alfresco repository is similar to that of nested folders or nodes. You start with a root folder and then add sub folders that can contain content or more folders. In Alfresco these folders are called spaces. Each space or piece of content has its own node reference.
In our scenario you have a standalone web application and want to be able to link to Alfresco’s Web Client from it. Alfresco will then handle all the content management functionality. You have objects in your web application for which you want a corresponding workspace in Alfresco to contain any content for that object. The functionality you need to add to your web application is:
1. When an object is created, create a corresponding workspace for that object in Alfresoc and retrieve the node reference for that workspace.
2. Store the objects and matching node references (you will need to do this yourself).
3. Select an object in your web application, get the node reference for the workspace in Alfresco, and open a new tab/window to view/update that workspace in Alfresco.
4. You also want to bypass the Alfresco login page / authentication in all these scenarios so the user doesn’t have to enter login details.
At a minimum you will need the following jars in your web application:
alfresco-web-service-client.jar
axis-1.4.jar
commons-discovery-0.2.jar
wsdl4j-1.6.2.jar
(jaxrpc.jar should already be available in a jboss application)
commons-logging-1.1.jar
wss4j.jar
opensaml-1.0.1.jar
xmlsec-1.4.1.jar
Depending on the jar files already included in your application you may need to add some more jars.
We’ll start with authenticating a web service call that will create a new space. To use a web service api you need to authenticate your session.
Authentication for web service API:
// Start the session
AuthenticationUtils.startSession(“admin”, “admin”);try {
// Make your web service call in here
}
catch (Throwable e) {
System.out.println(e.toString());
e.printStackTrace();
}
finally {
// End the session
AuthenticationUtils.endSession();
}
To create a workspace called “child” in the “parent” workspace using a web service call:
// The default workspace store is SpacesStore
Store storeRefence = new Store(Constants.WORKSPACE_STORE, “SpacesStore”);// We want to create a new folder or workspace called child in the parent folder under company home
// Create parent reference to the parent space in company home
ParentReference parentReference = new ParentReference(storeReference, null, “/app:company_home/cm:parent”,
Constants.ASSOC_CONTAINS, Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, “sample_folder”));// Create the child folder
NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, “Child Folder”) };
CMLCreate create = new CMLCreate(“1”, parentReference, null, null, null, Constants.TYPE_FOLDER, properties);
CML cml = new CML();
cml.setCreate(new CMLCreate[] { create });
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);// From the result of the web service call we want to retrieve the node reference for the child folder so we can access it later
String childReference = results[0].getDestination().getUuid();
System.out.println(“Created a new workspace, node is: ” + childReference);
In Part Four we will handle generating a url to access the space we have just created and bypassing the login page when accessing the url.
hi,
i read your topics on alfresco very knowledgeble.thanks for posting
i need ur suggestion as i am new to alfresco application.please tell me is it possible to “map data attributes to the templates during run time” in other way is it possibel to modify the template fields dynamically during runh time.
Thanks for your post . I was some how missing “Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, “sample_folder”));”
Your blog helped me right to the spot.
Thanks,
Raja
its good ,im new to alfresco , please help how to create folder inside multple documents .
im getting exception please check , posting code
package com.r2k;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.alfresco.util.ISO9075;
import org.alfresco.webservice.content.ContentServiceSoapBindingStub;
import org.alfresco.webservice.repository.QueryResult;
import org.alfresco.webservice.repository.RepositoryServiceSoapBindingStub;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLAddAspect;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.CMLDelete;
import org.alfresco.webservice.types.ContentFormat;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Predicate;
import org.alfresco.webservice.types.Query;
import org.alfresco.webservice.types.Reference;
import org.alfresco.webservice.types.ResultSet;
import org.alfresco.webservice.types.ResultSetRow;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.ContentUtils;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;
public class SampleTest extends HttpServlet {
private static final long serialVersionUID = 1L;
protected static final Store STORE = new Store(Constants.WORKSPACE_STORE, “SpacesStore”);
protected static final Reference SAMPLE_FOLDER = new Reference(STORE, null, “/app:company_home/cm:sample_folder”);
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String msg = “”;
try {
System.out.println(“inside try”);
String title = (String) request.getParameter(“title”).trim();
System.out.println(“title::::::” + title);
String description = request.getParameter(“description”).trim();
System.out.println(“description ::::” + description);
String path = request.getParameter(“file”);
System.out.println(“file name:::: ” + path);
path = path.replaceAll(“\\”, “/”);
String fileName = path.substring(path.lastIndexOf(‘/’) + 1, path
.length());
System.out.println(“file name:::: ” + fileName);
String ext = fileName.substring(fileName.lastIndexOf(‘.’) + 1,
fileName.length());
System.out.println(“ext name:::: ” + ext);
String contType = “”;
if (ext.equals(“txt”))
contType = “text/plain”;
else if (ext.equals(“xls”))
contType = “application/vnd.ms-excel”;
else if (ext.equals(“doc”))
contType = “application/msword”;
else if (ext.equals(“html”) || ext.equals(“htm”))
contType = “text/html”;
else if (ext.equals(“jpg”) || ext.equals(“jpeg”))
contType = “image/jpeg”;
else if (ext.equals(“bmp”))
contType = “image/bmp”;
else if (ext.equals(“pdf”))
contType = “application/pdf”;
else if (ext.equals(“ppt”))
contType = “application/vnd.ms-powerpoint”;
else if (ext.equals(“xml”))
contType = “text/xml”;
else if (ext.equals(“zip”))
contType = “application/vnd.ms-zip”;
else {
System.out.println(“inside alfresco part:::::::::”);
WebServiceFactory
.setEndpointAddress(“http://192.168.4.137:8080/alfresco/api”);
AuthenticationUtils.startSession(“admin”, “Password”);
System.out.println(“after login”);
try
{
// Check to see if the sample folder has already
// been created or not
WebServiceFactory.getRepositoryService().get(new Predicate(new Reference[]{SAMPLE_FOLDER}, STORE, null));
}
catch (Exception exception)
{
// Create parent reference to company home
ParentReference parentReference = new ParentReference(
STORE,
null,
“/app:company_home”,
Constants.ASSOC_CONTAINS,
Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, “sample_folder”));
// Create folder
NamedValue[] properties = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, “Web Service Sample Folder”)};
CMLCreate create = new CMLCreate(“1”, parentReference, null, null, null, Constants.TYPE_FOLDER, properties);
CML cml = new CML();
cml.setCreate(new CMLCreate[]{create});
UpdateResult[] results = WebServiceFactory.getRepositoryService().update(cml);
// Create parent reference to sample folder
String sampleFolder = results[0].getDestination().getUuid();
System.out.println(“sample folder”+sampleFolder);
ParentReference parentReference2 = new ParentReference(
STORE,
sampleFolder,
null,
Constants.ASSOC_CONTAINS,
Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, “sample_content”));
// Create content
NamedValue[] properties2 = new NamedValue[]{Utils.createNamedValue(Constants.PROP_NAME, fileName)};
CMLCreate create2 = new CMLCreate(“1”, parentReference2, null, null, null, Constants.TYPE_CONTENT, properties2);
CML cml2 = new CML();
cml2.setCreate(new CMLCreate[]{create2});
UpdateResult[] results2 = WebServiceFactory.getRepositoryService().update(cml2);
// Set content
ContentFormat format = new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, “UTF-8”);
byte[] content = “The Alfresco development team!”.getBytes();
WebServiceFactory.getContentService().write(results2[0].getDestination(), Constants.PROP_CONTENT, content, format);
}
}
}
catch (Throwable e)
{
msg = “Error uploading file”;
System.out.println(msg);
e.printStackTrace();
} finally {
AuthenticationUtils.endSession();
}
request.setAttribute(“msg”, msg);
RequestDispatcher rd = request
.getRequestDispatcher(“StatusMessage.jsp”);
rd.forward(request, response);
}
}