use of org.alfresco.service.cmr.repository.ContentIOException in project acs-community-packaging by Alfresco.
the class DownloadRawContentServlet method processRequest.
/**
* Processes the request using the current context i.e. no
* authentication checks are made, it is presumed they have already
* been done.
*
* @param req The HTTP request
* @param res The HTTP response
*/
private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String uri = req.getRequestURI();
String contentUrl = req.getParameter(ARG_CONTENT_URL);
if (contentUrl == null || contentUrl.length() == 0) {
throw new IllegalArgumentException("Download URL did not contain parameter '" + ARG_CONTENT_URL + "':" + uri);
}
String infoOnlyStr = req.getParameter(ARG_INFO_ONLY);
boolean infoOnly = (infoOnlyStr == null) ? false : Boolean.parseBoolean(infoOnlyStr);
ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
ContentService contentService = serviceRegistry.getContentService();
// Attempt to get the reader
ContentReader reader = null;
try {
reader = contentService.getRawReader(contentUrl);
// If the content doesn't exist, generate an error
if (!reader.exists()) {
if (logger.isDebugEnabled()) {
logger.debug("Returning 204 Not Found error...");
}
res.sendError(HttpServletResponse.SC_NO_CONTENT);
return;
}
} catch (AccessDeniedException e) {
if (logger.isDebugEnabled()) {
logger.debug("Returning 403 Forbidden error after exception: ", e);
}
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
long readerSize = reader.getSize();
Date readerLastModified = new Date(reader.getLastModified());
String readerMimetype = reader.getMimetype();
String readerEncoding = reader.getEncoding();
Locale readerLocale = reader.getLocale();
// Set the content info
res.setHeader("alfresco.dr.size", DefaultTypeConverter.INSTANCE.convert(String.class, readerSize));
res.setHeader("alfresco.dr.lastModified", DefaultTypeConverter.INSTANCE.convert(String.class, readerLastModified));
res.setHeader("alfresco.dr.mimetype", readerMimetype);
res.setHeader("alfresco.dr.encoding", readerEncoding);
res.setHeader("alfresco.dr.locale", DefaultTypeConverter.INSTANCE.convert(String.class, readerLocale));
// Pass the stream to the response, unless only the content info was requested
if (infoOnly) {
// Fill response details
res.setContentType(DEFAULT_MIMETYPE);
res.setCharacterEncoding(DEFAULT_ENCODING);
} else {
// Fill response details
res.setContentType(readerMimetype);
res.setCharacterEncoding(readerEncoding);
try {
OutputStream clientOs = res.getOutputStream();
// Streams closed for us
reader.getContent(clientOs);
} catch (SocketException e1) {
// Not a problem
if (logger.isDebugEnabled()) {
logger.debug("Client aborted stream read:\n" + " Content URL: " + contentUrl);
}
} catch (ContentIOException e2) {
// Not a problem
if (logger.isDebugEnabled()) {
logger.debug("Client aborted stream read:\n" + " Content URL: " + contentUrl);
}
}
}
}
use of org.alfresco.service.cmr.repository.ContentIOException in project SearchServices by Alfresco.
the class SolrFileContentWriter method putContent.
@Override
public synchronized void putContent(String content) throws ContentIOException {
try {
// attempt to use the correct encoding
String encoding = "UTF-8";
byte[] bytes = content.getBytes(encoding);
// get the stream
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
putContent(is);
// done
} catch (IOException e) {
throw new ContentIOException("Failed to copy content from string: \n" + " writer: " + this + " content length: " + content.length(), e);
}
}
use of org.alfresco.service.cmr.repository.ContentIOException in project SearchServices by Alfresco.
the class SolrFileContentWriter method putContent.
@Override
public synchronized void putContent(File sourceFile) throws ContentIOException {
if (written == true) {
throw new IllegalStateException("The writer has already been used: " + this.file);
} else if (this.file.exists()) {
throw new IllegalStateException("The file already exists: " + this.file);
} else if (!sourceFile.exists()) {
throw new IllegalStateException("The source file does not exist: " + sourceFile);
}
try {
FileUtils.copyFile(sourceFile, this.file, false);
written = true;
} catch (Throwable e) {
throw new ContentIOException("Failed to copy file onto file: " + sourceFile, e);
}
}
use of org.alfresco.service.cmr.repository.ContentIOException in project SearchServices by Alfresco.
the class SolrFileContentReader method getContentString.
@Override
public final String getContentString() throws ContentIOException {
try {
// read from the stream into a byte[]
InputStream is = getContentInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
// both streams are closed
FileCopyUtils.copy(is, os);
byte[] bytes = os.toByteArray();
// get the encoding for the string
String encoding = "UTF-8";
// create the string from the byte[] using encoding if necessary
String content = (encoding == null) ? new String(bytes) : new String(bytes, encoding);
// done
return content;
} catch (IOException e) {
throw new ContentIOException("Failed to copy content to string: \n" + " accessor: " + this, e);
}
}
use of org.alfresco.service.cmr.repository.ContentIOException in project alfresco-remote-api by Alfresco.
the class ContentStreamer method streamContentImpl.
/**
* Stream content implementation
*
* @param req The request
* @param res The response
* @param reader The reader
* @param nodeRef The content nodeRef if applicable
* @param propertyQName The content property if applicable
* @param attach Indicates whether the content should be streamed as an attachment or not
* @param modified Modified date of content
* @param eTag ETag to use
* @param attachFileName Optional file name to use when attach is <code>true</code>
* @throws IOException
*/
public void streamContentImpl(WebScriptRequest req, WebScriptResponse res, ContentReader reader, final NodeRef nodeRef, final QName propertyQName, final boolean attach, final Date modified, String eTag, final String attachFileName, Map<String, Object> model) throws IOException {
setAttachment(req, res, attach, attachFileName);
// establish mimetype
String mimetype = reader.getMimetype();
String extensionPath = req.getExtensionPath();
if (mimetype == null || mimetype.length() == 0) {
mimetype = MimetypeMap.MIMETYPE_BINARY;
int extIndex = extensionPath.lastIndexOf('.');
if (extIndex != -1) {
String ext = extensionPath.substring(extIndex + 1);
mimetype = mimetypeService.getMimetype(ext);
}
}
res.setHeader(HEADER_ACCEPT_RANGES, "bytes");
try {
boolean processedRange = false;
String range = req.getHeader(HEADER_CONTENT_RANGE);
final long size = reader.getSize();
final String encoding = reader.getEncoding();
if (range == null) {
range = req.getHeader(HEADER_RANGE);
}
if (range != null) {
if (logger.isDebugEnabled())
logger.debug("Found content range header: " + range);
// ensure the range header is starts with "bytes=" and process the range(s)
if (range.length() > 6) {
if (range.indexOf(',') != -1 && (nodeRef == null || propertyQName == null)) {
if (logger.isInfoEnabled())
logger.info("Multi-range only supported for nodeRefs");
} else {
HttpRangeProcessor rangeProcessor = new HttpRangeProcessor(contentService);
processedRange = rangeProcessor.processRange(res, reader, range.substring(6), nodeRef, propertyQName, mimetype, req.getHeader(HEADER_USER_AGENT));
}
}
}
if (processedRange == false) {
if (logger.isDebugEnabled())
logger.debug("Sending complete file content...");
// set mimetype for the content and the character encoding for the stream
res.setContentType(mimetype);
res.setContentEncoding(encoding);
// return the complete entity range
res.setHeader(HEADER_CONTENT_RANGE, "bytes 0-" + Long.toString(size - 1L) + "/" + Long.toString(size));
res.setHeader(HEADER_CONTENT_LENGTH, Long.toString(size));
// set caching
setResponseCache(res, modified, eTag, model);
// get the content and stream directly to the response output stream
// assuming the repository is capable of streaming in chunks, this should allow large files
// to be streamed directly to the browser response stream.
reader.getContent(res.getOutputStream());
}
} catch (SocketException e1) {
// the client cut the connection - our mission was accomplished apart from a little error message
if (logger.isInfoEnabled())
logger.info("Client aborted stream read:\n\tcontent: " + reader);
} catch (ContentIOException e2) {
if (logger.isInfoEnabled())
logger.info("Client aborted stream read:\n\tcontent: " + reader);
}
}
Aggregations