Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC (http://ursaj.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.ursaj.hfs.server;
 
 
 
 import static com.ursaj.hfs.client.HfsUploadStatus.SUCCEED;
 import static com.ursaj.hfs.server.HfsUploadLibrary.quote;

Uploads handling servlet.

Parameters:
<T> File information type your application works with.
 
 public abstract class HfsUploadServletAdapter<T> extends HfsServletAdapter<T> {
    
Class logger.
 
     private static final Logger log = LoggerFactory.getLogger(HfsUploadServletAdapter.class);

    
Handle and store upload information.

Parameters:
req HTTP request.
resp HTTP response to write JS library content to.
Throws:
javax.servlet.ServletException In case of any servlet exception.
java.io.IOException In case of any IO exception.
 
     @Override
     protected void doGet(HttpServletRequest reqHttpServletResponse respthrows ServletExceptionIOException {
         HfsUploadInfo uploadInfo;
 
         try {
             // Parse uploaded files information from the requested upload callback token.
             uploadInfo = getHfsClient().parseUploadInfo(getToken(req));
 
             // Store uploaded files.
             if (uploadInfo.getStatus() == )
                 storeUploadedFiles(reqrespuploadInfo.getFiles());
         }
         catch (Exception e) {
             .debug("Failed to store uploaded files information: {}"e.getMessage());
             .trace("Failed to store uploaded files information."e);
 
             // Wrap failure into client-side readable message.
             uploadInfo = new StoringFailedInfo("Failed to store uploaded files information"
                 + " (" + e.getMessage() + ")");
         }
 
         // Render upload frame.
         renderUploadFrame(reqrespuploadInfo);
     }

    
Store uploaded files information into your file's storage.

Parameters:
req HTTP request.
resp HTTP response to write result to.
uploadedFiles Uploaded files information.
Throws:
javax.servlet.ServletException In case of any servlet exception.
java.io.IOException In case of any IO exception.
 
     protected abstract void storeUploadedFiles(HttpServletRequest reqHttpServletResponse resp,
         Collection<HfsFileInfouploadedFilesthrows ServletExceptionIOException;

    
Render upload result frame, compatible with JS upload library.

Parameters:
req HTTP request.
resp HTTP response to write XSS result page to.
uploadInfo Upload information.
Throws:
javax.servlet.ServletException In case of any servlet exception.
java.io.IOException In case of any IO exception.
 
     protected void renderUploadFrame(HttpServletRequest reqHttpServletResponse respHfsUploadInfo uploadInfo)
         throws ServletExceptionIOException {
         // Render upload result frame, compatible with JS upload library.
         resp.setHeader("Content-Type""text/html");
 
        resp.getWriter().write(HfsUploadLibrary.renderUploadFrame(
            uploadInfo.getStatus().ordinal(), renderJsResponse(reqrespuploadInfo)));
    }

    
Render default JS response.

Parameters:
req HTTP request.
resp HTTP response to write XSS result page to.
uploadInfo Upload information.
Returns:
JS object to send to client, e.g. "{ message: 'OK', files: [ { id: 'id1', name: 'name1.txt' } ]"
Throws:
javax.servlet.ServletException In case of any servlet exception.
java.io.IOException In case of any IO exception.
    @SuppressWarnings("UnusedParameters")
    protected String renderJsResponse(HttpServletRequest reqHttpServletResponse respHfsUploadInfo uploadInfo)
        throws ServletExceptionIOException {
        StringBuilder files = new StringBuilder("[");
        String prefix = " ";
        for (HfsFileInfo info : uploadInfo.getFiles()) {
            files.append(prefix)
                .append("{ id: '").append(info.getFileId()).append("'")
                .append(", name: ").append(quote(info.getFileName()))
                .append(", size: ").append(info.getSize())
                .append(" }");
            prefix = ", ";
        }
        files.append("]");
        return ""
            + "{ code: " + quote(uploadInfo.getStatus().toString())
            + ", message: " + quote(uploadInfo.getMessage())
            + ", files: " + files
            + " }";
    }
New to GrepCode? Check out our FAQ X