Welcome, Guest. Please Login or Register
WebLab Project
  You can now retrieve WebLab Core sources from our OW2 SVN
  Warning ! This forum is now locked, new posts have been disabled. If you want to contact us about a problem or have question, please use the following mailing list:
user@weblab-project.org !
  HomeHelpSearchLoginRegister  
 
 
Page Index Toggle Pages: 1
Send Topic Print
[Resolved] Content provider and consumer (Read 1326 times)
30.04.2008 at 15:40:41

Jérémie Doucy   Offline
WebLab Administrator
Every nights I dream about
WebLab ...
Elancourt

Gender: male
Posts: 92
*****
 
Here is a very simple implementation of a content provider and a content consumer.
« Last Edit: 19.06.2008 at 19:02:52 by Yann Mombrun »  
IP Logged
 
Reply #1 - 30.04.2008 at 15:47:35

Jérémie Doucy   Offline
WebLab Administrator
Every nights I dream about
WebLab ...
Elancourt

Gender: male
Posts: 92
*****
 
Code:
package com.eads.weblab.services.content.provider.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.jws.WebService;

import org.weblab_project.core.model.content.BinaryContent;
import org.weblab_project.services.contentprovider.ContentProvider;
import org.weblab_project.services.contentprovider.GetContentException;
import org.weblab_project.services.contentprovider.types.GetContentArgs;
import org.weblab_project.services.contentprovider.types.GetContentReturn;
import org.weblab_project.services.exception.WebLabException;

@WebService(endpointInterface = "org.weblab_project.services.contentprovider.ContentProvider")
public class ContentProviderService implements ContentProvider {

	public GetContentReturn getContent(GetContentArgs args)
			throws GetContentException {
		GetContentReturn ret = new GetContentReturn();
		try {

			/*
			 * try to get a file corresponding to the contentId
			 */
			File contentFile = getFileFromContentId(args.getContentId());

			/*
			 * open a stream on this file
			 */
			FileInputStream fs = new FileInputStream(contentFile);
			byte[] tab = new byte[args.getLimit()];
			/*
			 * go to the offset
			 */
			fs.skip(args.getOffset());

			BinaryContent content = new BinaryContent();
			/*
			 * read bytes
			 */
			content.setLimit(fs.read(tab));
			/*
			 * setting read bytes to the content data field
			 */
			content.setData(tab);
			content.setUri(args.getContentId());

			fs.close();

			/*
			 * setting the content to the result wrapper
			 */
			ret.setContent(content);

		} catch (URINotBounded e) {
			/*
			 * when an URI is invalid, throwing a content not available exception
			 */
			WebLabException wExp = new WebLabException();
			wExp.setErrorId("E3");
			wExp.setErrorMessage("Content not available : uri '"
					+ args.getContentId() + "' not bounded.\n" + e.getMessage());
			GetContentException exp = new GetContentException("Content not available.", wExp);
			throw exp;

		} catch (IOException e) {
			/*
			 * when the offset is too big, throwing an invalid parameter exception
			 */
			WebLabException wExp = new WebLabException();
			wExp.setErrorId("E1");
			wExp.setErrorMessage("Invalid parameter : offset '"
					+ args.getOffset() + "' not reachable.\n" + e.getMessage());
			GetContentException exp = new GetContentException("Invalid parameter.", wExp);
			throw exp;
		}

		return ret;
	}

	/**
	 * Return the file matching the contentId
	 * @param contentId
	 * @return a file corresponding to the contentId
	 * @throws URINotBounded when the contentId is not valid for this service
	 */
	protected File getFileFromContentId(String contentId) throws URINotBounded {
		/*
		 * here is the specific code corresponding to your binary data persistence
		 */
		return new File("");
	}

	protected class URINotBounded extends Exception {
		private static final long serialVersionUID = 1546857L;
	}

}
 

 
IP Logged
 
Reply #2 - 30.04.2008 at 16:01:56

Jérémie Doucy   Offline
WebLab Administrator
Every nights I dream about
WebLab ...
Elancourt

Gender: male
Posts: 92
*****
 
Code:
package com.eads.weblab.services.content.provider.impl;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import javax.jws.WebService;

import org.weblab_project.core.model.content.BinaryContent;
import org.weblab_project.services.contentconsumer.ContentConsumer;
import org.weblab_project.services.contentconsumer.SetContentException;
import org.weblab_project.services.contentconsumer.types.SetContentArgs;
import org.weblab_project.services.contentconsumer.types.SetContentReturn;
import org.weblab_project.services.exception.WebLabException;

@WebService(endpointInterface = "org.weblab_project.services.contentconsumer.ContentConsumer")
public class ContentConsumerService implements ContentConsumer {

	public SetContentReturn setContent(SetContentArgs args)
			throws SetContentException {
		SetContentReturn ret = new SetContentReturn();

		if (args.getContent() instanceof BinaryContent) {
			try {
				BinaryContent content = (BinaryContent) args.getContent();

				File toBeSaved = getFileFromContentId(args.getContent()
						.getUri());
				RandomAccessFile raf = new RandomAccessFile(toBeSaved, "rw");

				raf.seek(content.getOffset());
				raf.write(content.getData(), 0, content.getLimit());
				raf.close();
			} catch (URINotBounded e) {
				/*
				 * when an URI is invalid, throwing a content not available exception
				 */
				WebLabException wExp = new WebLabException();
				wExp.setErrorId("E3");
				wExp.setErrorMessage("Content not available : uri '"
						+ args.getContent().getUri() + "' not bounded.\n"
						+ e.getMessage());
				SetContentException exp = new SetContentException(
						"Content not available.", wExp);
				throw exp;

			} catch (IOException e) {
				/*
				 * when the offset is too big, throwing an invalid parameter exception
				 */
				WebLabException wExp = new WebLabException();
				wExp.setErrorId("E1");
				wExp.setErrorMessage("Invalid parameter : offset '"
						+ args.getContent().getOffset() + "' not reachable.\n" + e.getMessage());
				SetContentException exp = new SetContentException("Invalid parameter.", wExp);
				throw exp;
			}
		} else {
			/*
			 * if the content is not a binary one throwing an invalid
			 * parameter exception
			 */
			WebLabException wExp = new WebLabException();
			wExp.setErrorId("E5");
			wExp.setErrorMessage("Unsupported request : '"
					+ args.getContent().getClass() + "' not a binary content.");
			SetContentException exp = new SetContentException(
					"Unsupported request.", wExp);
			throw exp;
		}

		return ret;
	}

	/**
	 * Return the file matching the contentId
	 *
	 * @param contentId
	 * @return a file corresponding to the contentId
	 * @throws URINotBounded
	 *		 when the contentId is not valid for this service
	 */
	protected File getFileFromContentId(String contentId) throws URINotBounded {
		/*
		 * here is the specific code corresponding to your binary data
		 * persistence
		 */
		return new File("");
	}

	protected class URINotBounded extends Exception {
		private static final long serialVersionUID = 1546857L;
	}

}
 

 
IP Logged
 
Reply #3 - 17.06.2008 at 12:36:49

lydie soler   Offline
Junior WebLab Member

Posts: 34
**
 
In your ContentConsumerService, you read the file using its URI. I though we read it from the data part of the Content object. (As bytes)
In my idea, ContentConsumerService wes the way to pass the document to a service....
I don't see how we can only use the URI....

Am I missing something?
 
IP Logged
 
Reply #4 - 17.06.2008 at 12:40:02

lydie soler   Offline
Junior WebLab Member

Posts: 34
**
 
Hum... maybe you use the URI to create a kind of local file and after you add the data in this file...

in this case, forget my previous question...
 
IP Logged
 
Reply #5 - 19.06.2008 at 19:01:45

Yann Mombrun   Offline
WebLab Administrator
I Love WebLab
Val de Reuil, France

Gender: male
Posts: 79
*****
 
Yes. For instance we are using it between our crawler service and our normaliser service.

The crawler is content provider, for every crawled file it send the binary to the normaliser. This content is stored in normaliser-side temp file.

Then in the normaliser code, we read the annotation to get the uri of the nativeContent, and since the temp file was created using this URI we can retrieve the whole binary content and process it.

After our normaliser, the document are containing mediaUnit with the right text extracted from the binary file.
 
IP Logged
 
Reply #6 - 13.08.2009 at 16:08:39

Celsia83   Offline
Junior WebLab Member
I Love WebLab
France

Gender: female
Posts: 18
**
 
Hi I'm using your Content Provider and ContentConsumer to send video data as a binary. I put your provider code in my client (portlet) and the Consumer as the service. I don't have errors when I compile but when I'm running the client/service and I try to upload a file I receive this error:

13:28:21,218 INFO  [PortletHotDeployListener:202] Registering portlets for hello
Portlets
13:28:21,218 WARN  [PortletLocalServiceImpl:345] Portlet with the name HelloPort
let1_WAR_helloPortlets is described in portlet.xml but does not have a matching
entry in liferay-portlet.xml
13:28:21,218 INFO  [PortletHotDeployListener:209] 1 portlets for helloPortlets a
re ready for registration
13:28:21,250 INFO  [PortletHotDeployListener:285] 1 portlets for helloPortlets r
egistered successfully
13:28:25,703 WARN  [PortletLocalServiceImpl:148] Portlet not found for 1 HelloPo
rtlet2_WAR_helloPortlets
13:28:25,703 WARN  [PortletLocalServiceImpl:148] Portlet not found for 1 HelloPo
rtlet3_WAR_helloPortlets
13:28:25,750 WARN  [PortletLocalServiceImpl:148] Portlet not found for 1 HelloPo
rtlet2_WAR_helloPortlets
13:28:25,750 WARN  [PortletLocalServiceImpl:148] Portlet not found for 1 HelloPo
rtlet3_WAR_helloPortlets
13:28:25,765 WARN  [PortletLocalServiceImpl:148] Portlet not found for 1 HelloPo
rtlet2_WAR_helloPortlets
13:28:25,765 WARN  [PortletLocalServiceImpl:148] Portlet not found for 1 HelloPo
rtlet3_WAR_helloPortlets
Received multipart/form-data; boundary=---------------------------17790175612429


setting the binary content
13:28:54,718 ERROR [jsp:60] com.sun.xml.ws.server.UnsupportedMediaException: Uns
upported Content-Type: text/html;charset=ISO-8859-1 Supported ones are: [text/xml]
com.sun.xml.ws.server.UnsupportedMediaException: Unsupported Content-Type: text/
html;charset=ISO-8859-1 Supported ones are: [text/xml]

       at com.sun.xml.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:2
91)
       at com.sun.xml.ws.encoding.StreamSOAPCodec.decode(StreamSOAPCodec.java:1
28)
       at com.sun.xml.ws.encoding.SOAPBindingCodec.decode(SOAPBindingCodec.java
:287)
       at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTr
ansportPipe.java:171)
       at com.sun.xml.ws.transport.http.client.HttpTransportPipe.processRequest
(HttpTransportPipe.java:86)
       at com.sun.xml.ws.transport.DeferredTransportPipe.processRequest(Deferre
dTransportPipe.java:116)
       at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:595)
       at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:554)
       at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:539)
       at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:436)
       at com.sun.xml.ws.client.Stub.process(Stub.java:248)
       at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:135)
       at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.
java:109)
       at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.
java:89)
       at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118)
       at $Proxy326.setContent(Unknown Source)
       at org.weblab_project.portlet.HelloPortlet1.processAction(HelloPortlet1.
java:250)


I don't understand why it says that I'm receiving text/html;charset=ISO-8859-1
in my portlet I set the  RenderResponse.setContentType("text/html");


here there is part of my code (client)



Code:
  BinaryContent content = new BinaryContent();
		try{
						FileInputStream fs = new FileInputStream(serverFile);
						byte[] tab = new byte[ Math.max((int)(serverFile.length() * 1.4),40) ];
						int length   = 0;
						int numBytes = 0;
						while( ( numBytes = fs.read( tab, length, 4096 ) ) >= 0 ) {
								length += numBytes;
							}  
						content.setLimit(numBytes);
						content.setData(tab);
						content.setUri("weblab://myportlet/mybinarydata");
						fs.close();
					}
					catch (IOException e){
						e.printStackTrace();
					}

		    }
		}
	  }
	  

		URL WSDLServiceURL = null;
		try {
			WSDLServiceURL = new File(getPortletContext().getRealPath(
			"WEB-INF/classes/wsdl/services/ResourceRepository.wsdl"))
			.toURL();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		  }  

		// creating service
		ResourceRepositoryService service = new ResourceRepositoryService(
		WSDLServiceURL,
		new QName(
		"http://weblab-project.org/services/resourcerepository",
		"ResourceRepositoryService"));
		ContentConsumer consumer = service.getContentconsumerPort();
		// setting the service URL, here service on local Tomcat
		setEndpointAddress(consumer, "http://localhost:8080/contentProject",
		"setContent");
		SetContentArgs in = new SetContentArgs();
		in.setContent(content);
		String result = new String();
		try {
			[b]// this is the line 250[/b]
			result = consumer.setContent(in).getContentId();
		}
		catch (SetContentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 




The server part is exactly as your implementation just adding the getFileFromContentId() method.

any ideas?
« Last Edit: 17.08.2009 at 16:01:42 by Yann Mombrun »  
IP Logged
 
Reply #7 - 20.08.2009 at 15:07:40

Jérémie Doucy   Offline
WebLab Administrator
Every nights I dream about
WebLab ...
Elancourt

Gender: male
Posts: 92
*****
 
Hello,
Sorry for the delay,
I think you should look at encoding problems, it looks you use  "charset=ISO-8859-1" somewhere, check that everything is in UTF-8 (client and server).

Cheers
 
IP Logged
 
Reply #8 - 21.08.2009 at 15:31:02

Celsia83   Offline
Junior WebLab Member
I Love WebLab
France

Gender: female
Posts: 18
**
 
yes, thanks, I missed this line <?xml version="1.0" encoding="UTF-8"?>

in the sun-jaxws.xml file
 
IP Logged
 
Page Index Toggle Pages: 1
Send Topic Print