Hi there,
I am struggling in implementing a (Web)Resource to load a file (not an
image) from an external source. Does anybody have/know of an example
that illustrates
* where/when to open the connection to the DB and start a transaction,
* fetch the file, and
* close the transaction?
I am trying to output to the browser a file from the Alfresco content
repository and need to perform similar steps.
Thanks,
Kaspar
P.S. Here's what I have, which results in ERROR - RequestCycle - Unable
to write the response.
public final class RepositoryFileResource extends WebResource
{
private static final long serialVersionUID = 8406533594955148061L;
private FileResourceStream stream;
public RepositoryFileResource()
{
setCacheable(true);
stream = new FileResourceStream();
}
@Override
protected void setHeaders(WebResponse response)
{
super.setHeaders(response);
}
@Override
public IResourceStream getResourceStream()
{
return this.stream;
}
@Override
protected int getCacheDuration()
{
return 48 * 3600; // 2 days
}
private final class FileResourceStream implements IResourceStream
{
private static final long serialVersionUID = 390065327841658269L;
private NodeRef nodeRef;
private transient RepositoryContext context;
private transient UserTransaction transaction;
private transient ContentReader reader;
private transient InputStream contentStream;
void prepare()
{
if (transaction != null)
return;
// Determine node to deliver
ValueMap params = getParameters();
String nid = params.getString("nid");
nodeRef = new NodeRef(KnowledgeCenterModel.STORE, nid);
// Get request cycle
context = RepositoryContext.get();
try
{
transaction =
context.transactionService.getNonPropagatingUserTransaction();
transaction.begin();
context.authenticationService.authenticate(Website.LOGIN,
Website.PASSWORD.toCharArray());
reader =
context.serviceRegistry.getContentService().getReader(nodeRef,
ContentModel.PROP_CONTENT);
}
catch (Exception e)
{
throw new RuntimeException("Error...", e);
}
}
public void close() throws IOException
{
if (contentStream != null)
contentStream.close();
try
{
if (transaction != null)
transaction.commit();
}
catch (Exception e)
{
}
finally
{
transaction = null;
}
}
public String getContentType()
{
prepare();
return reader.getMimetype();
}
public InputStream getInputStream() throws
ResourceStreamNotFoundException
{
prepare();
return contentStream = reader.getContentInputStream();
}
public Locale getLocale()
{
// TODO
return null;
}
public long length()
{
prepare();
return reader.getSize();
}
public void setLocale(Locale locale)
{
}
public Time lastModifiedTime()
{
// TODO
return null;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]