Author: glandais Date: 2008-02-11 15:09:20 +0000 (Mon, 11 Feb 2008) New Revision: 814 Added: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/SimExplorerServiceStream.java Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageService.java trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/service/MockStorageServiceImpl.java trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementExport.java Log: Simple stream handling of responses Added: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/SimExplorerServiceStream.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/SimExplorerServiceStream.java (rev 0) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/SimExplorerServiceStream.java 2008-02-11 15:09:20 UTC (rev 814) @@ -0,0 +1,160 @@ +/* +* ##% Copyright (C) 2008 Code Lutin, Gabriel Landais +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +* ##% */ +package fr.cemagref.simexplorer.is.service; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.Serializable; + +import com.healthmarketscience.rmiio.RemoteInputStream; +import com.healthmarketscience.rmiio.RemoteInputStreamClient; + +/** + * The Class SimExplorerServiceStream. + */ +public class SimExplorerServiceStream extends InputStream implements + Serializable { + + private static final long serialVersionUID = 1976003275577858320L; + + /** The ris. */ + private RemoteInputStream ris = null; + + /** The bis. */ + private BufferedInputStream bis = null; + + /** + * Instantiates a new sim explorer service stream. + * + * @param ris + * the ris + */ + public SimExplorerServiceStream(RemoteInputStream ris) { + super(); + this.ris = ris; + this.bis = null; + } + + /** + * Instantiates a new sim explorer service stream. + * + * @param bis + * the bis + */ + public SimExplorerServiceStream(InputStream bis) { + super(); + this.bis = new BufferedInputStream(bis); + } + + /** + * Inits the. + */ + private void init() { + if (bis == null) { + try { + this.bis = new BufferedInputStream(RemoteInputStreamClient + .wrap(ris)); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + /* (non-Javadoc) + * @see java.io.InputStream#available() + */ + @Override + public int available() throws IOException { + init(); + return bis.available(); + } + + /* (non-Javadoc) + * @see java.io.InputStream#close() + */ + @Override + public void close() throws IOException { + init(); + bis.close(); + } + + /* (non-Javadoc) + * @see java.io.InputStream#mark(int) + */ + @Override + public synchronized void mark(int readlimit) { + init(); + bis.mark(readlimit); + } + + /* (non-Javadoc) + * @see java.io.InputStream#markSupported() + */ + @Override + public boolean markSupported() { + init(); + return bis.markSupported(); + } + + /* (non-Javadoc) + * @see java.io.InputStream#read() + */ + @Override + public int read() throws IOException { + init(); + return bis.read(); + } + + /* (non-Javadoc) + * @see java.io.InputStream#read(byte[]) + */ + @Override + public int read(byte[] b) throws IOException { + init(); + return bis.read(b); + } + + /* (non-Javadoc) + * @see java.io.InputStream#read(byte[], int, int) + */ + @Override + public int read(byte[] b, int off, int len) throws IOException { + init(); + return bis.read(b, off, len); + } + + /* (non-Javadoc) + * @see java.io.InputStream#reset() + */ + @Override + public synchronized void reset() throws IOException { + init(); + bis.reset(); + } + + /* (non-Javadoc) + * @see java.io.InputStream#skip(long) + */ + @Override + public long skip(long n) throws IOException { + init(); + return bis.skip(n); + } + +} Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageService.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageService.java 2008-02-11 14:57:39 UTC (rev 813) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageService.java 2008-02-11 15:09:20 UTC (rev 814) @@ -17,6 +17,7 @@ * ##% */ package fr.cemagref.simexplorer.is.service; +import java.io.InputStream; import java.util.Map; import javax.ejb.Remote; @@ -123,7 +124,7 @@ throws SimExplorerServiceException; /** - * Export XML of element to a remote stream. + * Export XML of element to a stream. * * @param token * the token @@ -137,11 +138,11 @@ * @throws SimExplorerServiceException * the sim explorer service exception */ - RemoteInputStream retrieveElementXML(String token, String uuid, - String version) throws SimExplorerServiceException; + InputStream retrieveElementXML(String token, String uuid, String version) + throws SimExplorerServiceException; /** - * Export full element to a remote stream as a zip. + * Export full element to a stream as a zip. * * @param token * the token @@ -150,13 +151,13 @@ * @param version * the version * - * @return RMIIO stream with data + * @return the input stream * * @throws SimExplorerServiceException * the sim explorer service exception */ - RemoteInputStream retrieveElementFull(String token, String uuid, - String version) throws SimExplorerServiceException; + InputStream retrieveElementFull(String token, String uuid, String version) + throws SimExplorerServiceException; /** * Retrieve data related to an entity. @@ -170,14 +171,13 @@ * @param attachment * the attachment * - * @return the remote input stream + * @return the input stream * * @throws SimExplorerServiceException * the sim explorer service exception */ - RemoteInputStream retrieveElementData(String token, String uuid, - String version, Attachment attachment) - throws SimExplorerServiceException; + InputStream retrieveElementData(String token, String uuid, String version, + Attachment attachment) throws SimExplorerServiceException; /** * Export element to another service. Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java 2008-02-11 14:57:39 UTC (rev 813) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceClient.java 2008-02-11 15:09:20 UTC (rev 814) @@ -18,6 +18,10 @@ package fr.cemagref.simexplorer.is.service; import static org.codelutin.i18n.I18n._; + +import java.io.InputStream; + +import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; import fr.cemagref.simexplorer.is.storage.SimExplorerStorageException; import fr.cemagref.simexplorer.is.storage.engine.StorageEngine; @@ -74,4 +78,29 @@ } } + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementData(java.lang.String, java.lang.String, java.lang.String, fr.cemagref.simexplorer.is.attachment.Attachment) + */ + public InputStream retrieveElementData(String token, String uuid, + String version, Attachment attachment) + throws SimExplorerServiceException { + return retrieveElementDataCommon(token, uuid, version, attachment); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementFull(java.lang.String, java.lang.String, java.lang.String) + */ + public InputStream retrieveElementFull(String token, String uuid, + String version) throws SimExplorerServiceException { + return retrieveElementFullCommon(token, uuid, version); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementXML(java.lang.String, java.lang.String, java.lang.String) + */ + public InputStream retrieveElementXML(String token, String uuid, + String version) throws SimExplorerServiceException { + return retrieveElementXMLCommon(token, uuid, version); + } + } Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java 2008-02-11 14:57:39 UTC (rev 813) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceCommon.java 2008-02-11 15:09:20 UTC (rev 814) @@ -328,50 +328,34 @@ return elementSaved; } - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementData(java.lang.String, java.lang.String, java.lang.String, java.lang.String) - */ - public RemoteInputStream retrieveElementData(String token, String uuid, + protected InputStream retrieveElementDataCommon(String token, String uuid, String version, Attachment attachment) throws SimExplorerServiceException { try { MetaData mde = getMetadata(token, uuid, version); InputStream stream = getStorageEngine().retrieveData(token, mde, attachment); - RemoteInputStreamServer remoteStream = new SimpleRemoteInputStream( - stream); - return remoteStream.export(); + + return stream; } catch (SimExplorerStorageException e) { throw new SimExplorerServiceException(e); - } catch (RemoteException e) { - throw new SimExplorerServiceException(e); } } - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementXML(java.lang.String, java.lang.String, java.lang.String) - */ - public RemoteInputStream retrieveElementXML(String token, String uuid, + public InputStream retrieveElementXMLCommon(String token, String uuid, String version) throws SimExplorerServiceException { MetaData mde = getMetadata(token, uuid, version); try { InputStream xmlStream = getStorageEngine().retrieveData(token, mde, getXMLAttachment()); - RemoteInputStreamServer remoteStream = new SimpleRemoteInputStream( - xmlStream); - return remoteStream.export(); + return xmlStream; } catch (SimExplorerStorageException e) { throw new SimExplorerServiceException(e); - } catch (IOException e) { - throw new SimExplorerServiceException(e); } } - /* (non-Javadoc) - * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementFull(java.lang.String, java.lang.String, java.lang.String) - */ - public RemoteInputStream retrieveElementFull(String token, String uuid, + public InputStream retrieveElementFullCommon(String token, String uuid, String version) throws SimExplorerServiceException { LoggableElement element = getLoggableElement(token, uuid, version); MetaData mde = element.getMetaData(); @@ -398,9 +382,7 @@ ZipStreamEncoder zse = new ZipStreamEncoder(files, pipedOut); zse.start(); - RemoteInputStreamServer remoteStream = new SimpleRemoteInputStream( - pipedIn); - return remoteStream.export(); + return pipedIn; } catch (SimExplorerStorageException e) { throw new SimExplorerServiceException(e); } catch (IOException e) { Modified: trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java 2008-02-11 14:57:39 UTC (rev 813) +++ trunk/simexplorer-is/simexplorer-is-service/src/java/fr/cemagref/simexplorer/is/service/StorageServiceServer.java 2008-02-11 15:09:20 UTC (rev 814) @@ -17,12 +17,19 @@ * ##% */ package fr.cemagref.simexplorer.is.service; +import java.io.InputStream; +import java.rmi.RemoteException; + import javax.ejb.EJB; import javax.ejb.Remote; import javax.ejb.Stateless; import org.jboss.annotation.ejb.RemoteBinding; +import com.healthmarketscience.rmiio.RemoteInputStreamServer; +import com.healthmarketscience.rmiio.SimpleRemoteInputStream; + +import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.metadata.MetaData; import fr.cemagref.simexplorer.is.storage.SimExplorerStorageException; import fr.cemagref.simexplorer.is.storage.engine.StorageEngine; @@ -83,4 +90,51 @@ } } + /** + * Wrap stream. + * + * @param streamToWrap + * the stream to wrap + * + * @return the input stream + * + * @throws SimExplorerServiceException + * the sim explorer service exception + */ + private InputStream wrapStream(InputStream streamToWrap) + throws SimExplorerServiceException { + try { + RemoteInputStreamServer remoteStream = new SimpleRemoteInputStream( + streamToWrap); + return new SimExplorerServiceStream(remoteStream.export()); + } catch (RemoteException e) { + throw new SimExplorerServiceException(e); + } + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementData(java.lang.String, java.lang.String, java.lang.String, fr.cemagref.simexplorer.is.attachment.Attachment) + */ + public InputStream retrieveElementData(String token, String uuid, + String version, Attachment attachment) + throws SimExplorerServiceException { + return wrapStream(retrieveElementDataCommon(token, uuid, version, + attachment)); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementFull(java.lang.String, java.lang.String, java.lang.String) + */ + public InputStream retrieveElementFull(String token, String uuid, + String version) throws SimExplorerServiceException { + return wrapStream(retrieveElementFullCommon(token, uuid, version)); + } + + /* (non-Javadoc) + * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveElementXML(java.lang.String, java.lang.String, java.lang.String) + */ + public InputStream retrieveElementXML(String token, String uuid, + String version) throws SimExplorerServiceException { + return wrapStream(retrieveElementXMLCommon(token, uuid, version)); + } } Modified: trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/service/MockStorageServiceImpl.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/service/MockStorageServiceImpl.java 2008-02-11 14:57:39 UTC (rev 813) +++ trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/service/MockStorageServiceImpl.java 2008-02-11 15:09:20 UTC (rev 814) @@ -99,24 +99,24 @@ /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.service.StorageService#exportElement(java.lang.String, java.lang.String, java.lang.String) */ - public RemoteInputStream retrieveElementXML(String token, String uuid, + public InputStream retrieveElementXML(String token, String uuid, String version) throws SimExplorerServiceException { - return (RemoteInputStream) checkImplemented(); + return (InputStream) checkImplemented(); } /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.service.StorageService#exportFull(java.lang.String, java.lang.String, java.lang.String) */ - public RemoteInputStream retrieveElementFull(String token, String uuid, + public InputStream retrieveElementFull(String token, String uuid, String version) throws SimExplorerServiceException { - return (RemoteInputStream) checkImplemented(); + return (InputStream) checkImplemented(); } /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.service.StorageService#retrieveData(java.lang.String, java.lang.String, java.lang.String, java.lang.String) */ - public RemoteInputStream retrieveElementData(String token, String uuid, + public InputStream retrieveElementData(String token, String uuid, String version, Attachment attachment) throws SimExplorerServiceException { return null; @@ -269,4 +269,11 @@ checkImplemented(); } + @Override + public Version exportElementTo(String token, StorageService otherService, + String uuid, String version) throws SimExplorerServiceException { + checkImplemented(); + return null; + } + } Modified: trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java 2008-02-11 14:57:39 UTC (rev 813) +++ trunk/simexplorer-is/simexplorer-is-swing/src/java/fr/cemagref/simexplorer/is/ui/StorageServiceHelper.java 2008-02-11 15:09:20 UTC (rev 814) @@ -118,31 +118,23 @@ public static BufferedInputStream retrieveElementFull(SimExplorerContext context, boolean remote, String uuid, String version) { try { - RemoteInputStream ris; - ris = getService(context, remote).retrieveElementFull(context.getToken(), uuid, version); - InputStream stream = RemoteInputStreamClient.wrap(ris); + InputStream stream = getService(context, remote).retrieveElementFull(context.getToken(), uuid, version); BufferedInputStream bis; bis = new BufferedInputStream(stream); return bis; } catch (SimExplorerServiceException e1) { throw new SimExplorerRuntimeException(e1); - } catch (IOException e) { - throw new SimExplorerRuntimeException(e); } } public static BufferedInputStream retrieveElementData(SimExplorerContext context, boolean remote, String uuid, String version, Attachment attachment) { try { - RemoteInputStream ris; - ris = getService(context, remote).retrieveElementData(context.getToken(), uuid, version, attachment); - InputStream stream = RemoteInputStreamClient.wrap(ris); + InputStream stream = getService(context, remote).retrieveElementData(context.getToken(), uuid, version, attachment); BufferedInputStream bis; bis = new BufferedInputStream(stream); return bis; } catch (SimExplorerServiceException e1) { throw new SimExplorerRuntimeException(e1); - } catch (IOException e) { - throw new SimExplorerRuntimeException(e); } } Modified: trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementExport.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementExport.java 2008-02-11 14:57:39 UTC (rev 813) +++ trunk/simexplorer-is/simexplorer-is-web/src/java/fr/cemagref/simexplorer/is/ui/web/pages/ElementExport.java 2008-02-11 15:09:20 UTC (rev 814) @@ -26,9 +26,6 @@ import org.apache.tapestry.StreamResponse; import org.codelutin.tapestry.beans.TreeNode; -import com.healthmarketscience.rmiio.RemoteInputStream; -import com.healthmarketscience.rmiio.RemoteInputStreamClient; - import fr.cemagref.simexplorer.is.attachment.Attachment; import fr.cemagref.simexplorer.is.entities.data.Component; import fr.cemagref.simexplorer.is.entities.data.ExplorationApplication; @@ -67,10 +64,9 @@ MetaData mde = getMetadata(); try { - RemoteInputStream ris = RemoteStorageService.getStorageService() + InputStream stream = RemoteStorageService.getStorageService() .retrieveElementXML(getToken(), mde.getUuid(), mde.getVersion().toString()); - InputStream stream = RemoteInputStreamClient.wrap(ris); response = new XMLAttachment(stream, mde.getType() + "." + mde.getUuid() + ".v" + mde.getVersion()); } catch (Exception e) { @@ -91,10 +87,9 @@ MetaData mde = getMetadata(); try { - RemoteInputStream ris = RemoteStorageService.getStorageService() + InputStream stream = RemoteStorageService.getStorageService() .retrieveElementFull(getToken(), mde.getUuid(), mde.getVersion().toString()); - InputStream stream = RemoteInputStreamClient.wrap(ris); response = new ZipAttachment(stream, mde.getType() + "." + mde.getUuid() + ".v" + mde.getVersion()); } catch (Exception e) { @@ -126,10 +121,9 @@ List<Attachment> attachments = metadata.getAttachments(); for (Attachment attachment : attachments) { if (attachment.getUniqueId().equals(attachmentUniqueId)) { - RemoteInputStream ris = RemoteStorageService - .getStorageService().retrieveElementData(getToken(), uuid, - version, attachment); - InputStream stream = RemoteInputStreamClient.wrap(ris); + InputStream stream = RemoteStorageService + .getStorageService().retrieveElementData( + getToken(), uuid, version, attachment); response = new AttachmentStreamResponse(stream, attachment .getFileName());