Search in sources :

Example 1 with ContentRange

use of org.apache.tomcat.util.http.parser.ContentRange in project tomee by apache.

the class DefaultServlet method parseContentRange.

/**
 * Parse the content-range header.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @return the partial content-range, {@code null} if the content-range
 *         header was invalid or {@code #IGNORE} if there is no header to
 *         process
 * @throws IOException an IO error occurred
 */
protected Range parseContentRange(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Retrieving the content-range header (if any is specified
    String contentRangeHeader = request.getHeader("Content-Range");
    if (contentRangeHeader == null) {
        return IGNORE;
    }
    if (!allowPartialPut) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    ContentRange contentRange = ContentRange.parse(new StringReader(contentRangeHeader));
    if (contentRange == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    // bytes is the only range unit supported
    if (!contentRange.getUnits().equals("bytes")) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    // TODO: Remove the internal representation and use Ranges
    // Convert to internal representation
    Range range = new Range();
    range.start = contentRange.getStart();
    range.end = contentRange.getEnd();
    range.length = contentRange.getLength();
    if (!range.validate()) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    return range;
}
Also used : ContentRange(org.apache.tomcat.util.http.parser.ContentRange) StringReader(java.io.StringReader) ContentRange(org.apache.tomcat.util.http.parser.ContentRange)

Example 2 with ContentRange

use of org.apache.tomcat.util.http.parser.ContentRange in project tomcat by apache.

the class DefaultServlet method doPut.

/**
 * Process a PUT request for the specified resource.
 *
 * @param req The servlet request we are processing
 * @param resp The servlet response we are creating
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet-specified error occurs
 */
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    if (readOnly) {
        sendNotAllowed(req, resp);
        return;
    }
    String path = getRelativePath(req);
    WebResource resource = resources.getResource(path);
    ContentRange range = parseContentRange(req, resp);
    if (range == null) {
        // Processing error. parseContentRange() set the error code
        return;
    }
    InputStream resourceInputStream = null;
    try {
        // Assume just one range is specified for now
        if (range == IGNORE) {
            resourceInputStream = req.getInputStream();
        } else {
            File contentFile = executePartialPut(req, range, path);
            resourceInputStream = new FileInputStream(contentFile);
        }
        if (resources.write(path, resourceInputStream, true)) {
            if (resource.exists()) {
                resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
            } else {
                resp.setStatus(HttpServletResponse.SC_CREATED);
            }
        } else {
            resp.sendError(HttpServletResponse.SC_CONFLICT);
        }
    } finally {
        if (resourceInputStream != null) {
            try {
                resourceInputStream.close();
            } catch (IOException ioe) {
            // Ignore
            }
        }
    }
}
Also used : ContentRange(org.apache.tomcat.util.http.parser.ContentRange) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) WebResource(org.apache.catalina.WebResource) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 3 with ContentRange

use of org.apache.tomcat.util.http.parser.ContentRange in project tomcat by apache.

the class DefaultServlet method parseContentRange.

/**
 * Parse the content-range header.
 *
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @return the partial content-range, {@code null} if the content-range
 *         header was invalid or {@code #IGNORE} if there is no header to
 *         process
 * @throws IOException an IO error occurred
 */
protected ContentRange parseContentRange(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Retrieving the content-range header (if any is specified
    String contentRangeHeader = request.getHeader("Content-Range");
    if (contentRangeHeader == null) {
        return IGNORE;
    }
    if (!allowPartialPut) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    ContentRange contentRange = ContentRange.parse(new StringReader(contentRangeHeader));
    if (contentRange == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    if (!validate(contentRange)) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return null;
    }
    return contentRange;
}
Also used : ContentRange(org.apache.tomcat.util.http.parser.ContentRange) StringReader(java.io.StringReader)

Aggregations

ContentRange (org.apache.tomcat.util.http.parser.ContentRange)3 StringReader (java.io.StringReader)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)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 WebResource (org.apache.catalina.WebResource)1