Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 Ursa Project LLC ( 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 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;
This servlet adapter allows you to use HFS client and your application file service during requests handling.

HFS client name is resolved from the servlet (or context as fallback) initial parameter with name CLIENT_NAME_INIT_PARAMETER.

Servlet level (web.xml):

Context level (web.xml):

File service is resolved from the context attribute with name FILE_SERVICE_CONTEXT_ATTRIBUTE. Its the task of your application to provide correct file service on servlet initialization step.

Both HFS client and file service are initialized during initialization step in init(javax.servlet.ServletConfig) method.

<T> File information type your application works with.
 public abstract class HfsServletAdapter<T> extends HttpServlet {
HFS client name initial servlet parameter.
     public static final String CLIENT_NAME_INIT_PARAMETER = "hfs.client";

File service servlet context attribute name to use for garbage collection procedures.
     public static final String FILE_SERVICE_CONTEXT_ATTRIBUTE = "file.service";

HTTP request parameter name of the encoded HFS server communication message.
     public static final String ENCODED_MESSAGE_HTTP_PARAMETER = "token";

HFS client to use in servlet.
     private volatile HfsClient client;

File service to use in servlet.
     private volatile HfsFileService<T> fileService;
     public void init(ServletConfig configthrows ServletException {
         String clientName = config.getInitParameter();
         if (clientName == null || clientName.isEmpty())
             clientName = config.getServletContext().getInitParameter();
         if (clientName == null || clientName.isEmpty())
             throw new ServletException("Missed required initial parameter neither in servlet nor in context"
                 + " '" +  + "' with HFS client name.");
          = HfsFactory.getClient(clientName);
         //noinspection unchecked
         if ( == null)
             throw new ServletException("Missed required servlet context attribute"
                 + " '" +  + "' with file service to use in servlet.");

Get HFS client instance.

HFS client instance.
    protected HfsClient getHfsClient() {
        return ;

Get file service instance.

File service instance.
    protected HfsFileService<T> getFileService() {
        return ;

Get encoded HFS server communication message (token).

request Request to get communication message from.
Encoded HFS server communication message (token).
javax.servlet.ServletException In case of any servlet exception. In case of any IO exception.
    protected String getToken(HttpServletRequest requestthrows ServletExceptionIOException {
        String token = request.getParameter();
        if (token == null)
            throw new IOException("Missed request parameter"
                + " '" +  + "' with encoded communication message from HFS server.");
        return token;

Recover absolute application URI for passed request, e.g.:
  • "" - for root context;
  • "http://server-name:11200/messaging" - for context "/messaging".

req HTTP servlet request.
Absolute context URI for passed request.
    protected static String getApplicationUri(HttpServletRequest req) {
        // Reconstruct original requesting URI.
        StringBuilder uri = new StringBuilder();
        String scheme = req.getScheme();
        if (scheme != null && !scheme.isEmpty())
        int port = req.getServerPort();
        if (!(port == 0 || port == 80 && "http".equals(scheme) || port == 443 && "https".equals(scheme)))
        String contextPath = req.getContextPath();
        if (contextPath != null && !contextPath.isEmpty())
        return uri.toString();
New to GrepCode? Check out our FAQ X