ServletUtil.java :  » Math » xsonymathew » com » xsm » servlet » Java Open Source

Java Open Source » Math » xsonymathew 
xsonymathew » com » xsm » servlet » ServletUtil.java
/*
 * Created on Feb 21, 2008
 *
 * Copyright (c) 2007 Prime Therapeutics LLC. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Prime 
 * Therapeutics LLC. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the employment agreement you entered into
 * with Prime Therapeutics LLC.
 */
package com.xsm.servlet;

import java.net.MalformedURLException;

import javax.servlet.http.HttpServletRequest;

import com.xsm.lite.util.StringUtil;

/**
 * Provides simple utilities for a Servlet or Filter.
 * 
 * @author Sony Mathew
 */
public class ServletUtil {

  /**
   * Given an HTTP Request URL - obtains the uri portion of the path making this request.
   *   It gives everything after the contextRoot including the first '/' after contextRoot if it exists.
   * 
   * if URL is http:/myhost:port/contextRoot/mainPath/extra1/extra2/
   *     will return /mainPath/extra1/extra2/ including the first '/' after contextRoot if it exists. 
   * 
   * Note: Probably could rely on servletPath and PathInfo - but they are inconsisted between spec versions. 
   *   so relying on HttpServletRequest.getRequestURI() - which is still consistent.
   * 
   * author Sony Mathew
   */
  public static String getRequestPath(HttpServletRequest httpRequest) throws MalformedURLException {
    String uri = httpRequest.getRequestURI();
    
    if (uri == null || (uri=uri.trim()).length()==0) {
      throw new MalformedURLException(uri);      
    }
    
    if (! uri.startsWith(httpRequest.getContextPath())) {
      throw new MalformedURLException(uri);
    }
    
    return uri.substring(httpRequest.getContextPath().length());     
  }

  /**
   * Tokenizes a requestPath
   * 
   * @see #getRequestPath(HttpServletRequest)
   * 
   * author Sony Mathew
   */
  public static String[] getTokenizedRequestPath(HttpServletRequest httpRequest) throws MalformedURLException {
    return StringUtil.pathSplit(getRequestPath(httpRequest));    
  }
  
  /**
   * Tokenizes a requestPath and gives the mainPath and the extraPaths.
   * 
   * @see ServletUtil.getRequestPath(HttpServletRequest).
   * 
   * @author Sony Mathew
   */
  public static class RequestPathInfo {
    public String mainPath = "";
    public String[] extraPaths = new String[0];
  }
  
  /**
   * Tokenizes a requestPath into a RequestInfo object.
   * 
   * @see #getRequestPath(HttpServletRequest)
   * 
   * author Sony Mathew
   */
  public static RequestPathInfo getRequestPathInfo(HttpServletRequest req) throws MalformedURLException {
    RequestPathInfo pi = new RequestPathInfo();
    
    String[] requestTokens = ServletUtil.getTokenizedRequestPath(req);
    
    if (requestTokens != null && requestTokens.length > 0) {
      pi.mainPath = requestTokens[0];
      if (requestTokens.length > 1) {
        pi.extraPaths = new String[requestTokens.length - 1];
        System.arraycopy(requestTokens, 1, pi.extraPaths, 0, pi.extraPaths.length);      
      }
    }
    
    return pi;
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.