Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Proxy Servlet for submitting files

I'm attempting to use Panda with my GWT application. I can upload videos directly to my panda server using

POST MY_PANDA_SERVER/videos/MY_VIDEO_ID/upload

However I would like hide my panda server behind my J2EE (glassfish) server. I would like to achieve this:

  1. Start upload to some servlet on my J2EE server
  2. Authenticate user
  3. POST the file to my panda server while still uploading to servlet

Ideally I would like to never store the file on the J2EE server, but just use it as a proxy to get to the panda server.

like image 256
KevMo Avatar asked Mar 18 '10 16:03

KevMo


Video Answer


1 Answers

Commons FileUpload is nice, but not sufficient in your case. It will parse the entire body in memory before providing the file items (and streams). You're not interested in the individual items. You basically just want to stream the request body from the one to other side transparently without altering it or storing it in memory in any way. FileUpload would only parse the request body into some "useable" Java objects and HttpClient would only create the very same request body again based on those Java objects. Those Java objects consumes memory as well.

You don't need a library for this (or it must be Commons IO to replace the for loop with an oneliner using IOUtils#copy()). Just the basic Java NET and IO API's suffices. Here's a kickoff example:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    URLConnection connection = new URL("http://your.url.to.panda").openConnection();
    connection.setDoOutput(true); // POST.
    connection.setRequestProperty("Content-Type", request.getHeader("Content-Type")); // This one is important! You may want to check other request headers and copy it as well.

    // Set streaming mode, else HttpURLConnection will buffer everything.
    int contentLength = request.getContentLength();
    if (contentLength > -1) {
        // Content length is known beforehand, so no buffering will be taken place.
        ((HttpURLConnection) connection).setFixedLengthStreamingMode(contentLength);
     } else {
        // Content length is unknown, so send in 1KB chunks (which will also be the internal buffer size).
        ((HttpURLConnection) connection).setChunkedStreamingMode(1024);
    }

    InputStream input = request.getInputStream();
    OutputStream output = connection.getOutputStream();
    byte[] buffer = new byte[1024]; // Uses only 1KB of memory!

    for (int length = 0; (length = input.read(buffer)) > 0;) {
        output.write(buffer, 0, length);
        output.flush();
    }

    output.close();
    connection.getInputStream(); // Important! It's lazily executed.
}
like image 72
BalusC Avatar answered Oct 22 '22 04:10

BalusC