use of org.apache.commons.io.input.TeeInputStream in project opencast by opencast.
the class WorkspaceImpl method put.
@Override
public URI put(String mediaPackageID, String mediaPackageElementID, String fileName, InputStream in) throws IOException {
String safeFileName = PathSupport.toSafeName(fileName);
final URI uri = wfr.getURI(mediaPackageID, mediaPackageElementID, fileName);
notNull(in, "in");
// Determine the target location in the workspace
File workspaceFile = null;
FileOutputStream out = null;
synchronized (lock) {
workspaceFile = toWorkspaceFile(uri);
FileUtils.touch(workspaceFile);
}
// Try hard linking first and fall back to tee-ing to both the working file repository and the workspace
if (linkingEnabled) {
// The WFR stores an md5 hash along with the file, so we need to use the API and not try to write (link) the file
// there ourselves
wfr.put(mediaPackageID, mediaPackageElementID, fileName, in);
File workingFileRepoDirectory = workingFileRepositoryFile(WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mediaPackageID, mediaPackageElementID);
File workingFileRepoCopy = new File(workingFileRepoDirectory, safeFileName);
FileSupport.link(workingFileRepoCopy, workspaceFile, true);
} else {
InputStream tee = null;
try {
out = new FileOutputStream(workspaceFile);
tee = new TeeInputStream(in, out, true);
wfr.put(mediaPackageID, mediaPackageElementID, fileName, tee);
} finally {
IOUtils.closeQuietly(tee);
IOUtils.closeQuietly(out);
}
}
// wait until the file appears on the WFR node
waitForResource(uri, HttpServletResponse.SC_OK, "File %s does not appear in WFR");
return uri;
}
use of org.apache.commons.io.input.TeeInputStream in project opencast by opencast.
the class WorkspaceImpl method putInCollection.
@Override
public URI putInCollection(String collectionId, String fileName, InputStream in) throws IOException {
String safeFileName = PathSupport.toSafeName(fileName);
URI uri = wfr.getCollectionURI(collectionId, fileName);
// Determine the target location in the workspace
InputStream tee = null;
File tempFile = null;
FileOutputStream out = null;
try {
synchronized (lock) {
tempFile = toWorkspaceFile(uri);
FileUtils.touch(tempFile);
out = new FileOutputStream(tempFile);
}
// Try hard linking first and fall back to tee-ing to both the working file repository and the workspace
if (linkingEnabled) {
tee = in;
wfr.putInCollection(collectionId, fileName, tee);
FileUtils.forceMkdir(tempFile.getParentFile());
File workingFileRepoDirectory = workingFileRepositoryFile(WorkingFileRepository.COLLECTION_PATH_PREFIX, collectionId);
File workingFileRepoCopy = new File(workingFileRepoDirectory, safeFileName);
FileSupport.link(workingFileRepoCopy, tempFile, true);
} else {
tee = new TeeInputStream(in, out, true);
wfr.putInCollection(collectionId, fileName, tee);
}
} catch (IOException e) {
FileUtils.deleteQuietly(tempFile);
throw e;
} finally {
IoSupport.closeQuietly(tee);
IoSupport.closeQuietly(out);
}
waitForResource(uri, HttpServletResponse.SC_OK, "File %s does not appear in WFR");
return uri;
}
use of org.apache.commons.io.input.TeeInputStream in project application by collectionspace.
the class ReturnedMultipartDocument method setResponse.
@Override
public boolean setResponse(HttpMethod method, int status) throws Exception {
// it's ok to release the parent connection since we consume the entire response stream here
boolean result = true;
setStatus(status);
InputStream stream = method.getResponseBodyAsStream();
SAXReader reader = new SAXReader();
if (isErrorStatus()) {
String streamMessage = IOUtils.toString(stream);
String Credentials401Error = "Incorrect of missing credentials. This request requires HTTP authentication.";
String msg = String.format("%s request failed with status code %s: %s: %s", method.getName(), status, status == 401 ? Credentials401Error : streamMessage, method.getURI());
log.warn(msg);
}
// TODO errorhandling
Document doc = null;
Header content_type = method.getResponseHeader("Content-Type");
if (content_type != null && "application/xml".equals(content_type.getValue())) {
if (log.isDebugEnabled()) {
ByteArrayOutputStream dump = new ByteArrayOutputStream();
doc = reader.read(new TeeInputStream(stream, dump));
log.debug(dump.toString("UTF-8"));
} else {
doc = reader.read(stream, "UTF-8");
}
// split up document
Element root = doc.getRootElement();
// iterate through child elements of root
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
addDocument(element.getName(), DocumentHelper.parseText(element.asXML()));
}
}
stream.close();
return result;
}
use of org.apache.commons.io.input.TeeInputStream in project application by collectionspace.
the class WebUIRequest method initRequest.
private void initRequest(UIUmbrella umbrella, HttpServletRequest request, HttpServletResponse response, List<String> p) throws IOException, UIException {
this.request = request;
this.response = response;
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
FileItemIterator iter;
try {
iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
// InputStream stream = item.openStream();
if (item.isFormField()) {
// System.out.println("Form field " + name + " with value "
// + Streams.asString(stream) + " detected.");
} else {
// System.out.println("File field " + name + " with file name "
// + item.getName() + " detected.");
// Process the input stream
contentHeaders = item.getHeaders();
uploadName = item.getName();
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
if (item != null) {
InputStream stream = item.openStream();
IOUtils.copy(stream, byteOut);
new TeeInputStream(stream, byteOut);
}
bytebody = byteOut.toByteArray();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
body = IOUtils.toString(request.getInputStream(), "UTF-8");
}
this.ppath = p.toArray(new String[0]);
if (!(umbrella instanceof WebUIUmbrella))
throw new UIException("Bad umbrella");
this.umbrella = (WebUIUmbrella) umbrella;
session = calculateSessionId();
}
Aggregations