Search in sources :

Example 1 with RandomFileOutputStream

use of io.milton.common.RandomFileOutputStream in project lobcder by skoulouzis.

the class PutHandler method processReplace.

/**
 * "If an existing resource is modified, either the 200 (OK) or 204 (No
 * Content) response codes SHOULD be sent to indicate successful completion
 * of the request."
 *
 * @param request
 * @param response
 * @param replacee
 */
private void processReplace(HttpManager manager, Request request, Response response, ReplaceableResource replacee) throws BadRequestException, NotAuthorizedException, ConflictException, NotFoundException {
    if (!handlerHelper.checkAuthorisation(manager, replacee, request)) {
        responseHandler.respondUnauthorised(replacee, response, request);
        return;
    }
    try {
        Range range = putHelper.parseContentRange(replacee, request);
        if (range != null) {
            log.debug("partial put: " + range);
            if (replacee instanceof PartialllyUpdateableResource) {
                log.debug("doing partial put on a PartialllyUpdateableResource");
                PartialllyUpdateableResource partialllyUpdateableResource = (PartialllyUpdateableResource) replacee;
                partialllyUpdateableResource.replacePartialContent(range, request.getInputStream());
            } else if (replacee instanceof GetableResource) {
                log.debug("doing partial put on a GetableResource");
                File tempFile = File.createTempFile("milton-partial", null);
                RandomAccessFile randomAccessFile = null;
                // The new length of the resource
                long length;
                try {
                    randomAccessFile = new RandomAccessFile(tempFile, "rw");
                    RandomFileOutputStream tempOut = new RandomFileOutputStream(tempFile);
                    GetableResource gr = (GetableResource) replacee;
                    // Update the content with the supplied partial content, and get the result as an inputstream
                    gr.sendContent(tempOut, null, null, null);
                    // Calculate new length, if the partial put is extending it
                    length = randomAccessFile.length();
                    if (range.getFinish() + 1 > length) {
                        length = range.getFinish() + 1;
                    }
                    randomAccessFile.setLength(length);
                    randomAccessFile.seek(range.getStart());
                    int numBytesRead;
                    byte[] copyBuffer = new byte[1024];
                    InputStream newContent = request.getInputStream();
                    while ((numBytesRead = newContent.read(copyBuffer)) != -1) {
                        randomAccessFile.write(copyBuffer, 0, numBytesRead);
                    }
                } finally {
                    FileUtils.close(randomAccessFile);
                }
                InputStream updatedContent = new FileInputStream(tempFile);
                BufferedInputStream bufin = new BufferedInputStream(updatedContent);
                // Now, finally, we can just do a normal update
                replacee.replaceContent(bufin, length);
            } else {
                throw new BadRequestException(replacee, "Cant apply partial update. Resource does not support PartialllyUpdateableResource or GetableResource");
            }
        } else {
            // Not a partial update, but resource implements Replaceable, so give it the new data
            Long l = request.getContentLengthHeader();
            replacee.replaceContent(request.getInputStream(), l);
        }
    } catch (IOException ex) {
        log.warn("IOException reading input stream. Probably interrupted upload: " + ex.getMessage());
        return;
    }
    // Respond with a 204
    responseHandler.respondNoContent(replacee, response, request);
    log.debug("process: finished");
}
Also used : RandomFileOutputStream(io.milton.common.RandomFileOutputStream) RandomAccessFile(java.io.RandomAccessFile) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GetableResource(io.milton.resource.GetableResource) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) Range(io.milton.http.Range) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

RandomFileOutputStream (io.milton.common.RandomFileOutputStream)1 Range (io.milton.http.Range)1 BadRequestException (io.milton.http.exceptions.BadRequestException)1 GetableResource (io.milton.resource.GetableResource)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1