Package de.odysseus.calyxo.control.base

Source Code of de.odysseus.calyxo.control.base.ControlModuleMapping

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* 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 de.odysseus.calyxo.control.base;

import javax.servlet.http.HttpServletRequest;

import de.odysseus.calyxo.base.conf.ConfigException;

/**
* Module mapping.
* Requests may be mapped to a module by a prefix and/or extension.
* This class encapsulates a servlet mapping and allows mapping between
* (context-relative) requests paths and module-relative action paths
* and vice versa.
*
* @author Christoph Beck
*/
public class ControlModuleMapping {

  /**
   * The request attribute under which the path information is stored for
   * processing during a RequestDispatcher.include() call.
   */
  private static final String INCLUDE_PATH_INFO =
    "javax.servlet.include.path_info";

  /**
   * The request attribute under which the servlet path information is stored
   * for processing during a RequestDispatcher.include() call.
   */
  private static final String INCLUDE_SERVLET_PATH =
    "javax.servlet.include.servlet_path";


  /**
   * Get the servlet path from specified request.
   */
  private static final String getServletPath(HttpServletRequest request) {
    String path = (String)request.getAttribute(INCLUDE_SERVLET_PATH);
    if (path == null)
      path = request.getServletPath();
    return path;
  }

  /**
   * Get the path info from specified request.
   */
  private static final String getPathInfo(HttpServletRequest request) {
    String path = (String)request.getAttribute(INCLUDE_PATH_INFO);
    if (path == null)
      path = request.getPathInfo();
    return path;
  }

  private String prefix;
  private String extension;

  /**
   * Constructor.
   */
  public ControlModuleMapping(String prefix, String extension) {
    this.prefix = prefix;
    this.extension = extension;
  }

  /**
   * Constructor.
   * @param pattern servlet mapping pattern as specified in web.xml
   * @throws ConfigException
   */
  public ControlModuleMapping(String pattern) throws ConfigException {
    if (pattern.startsWith("*.")) {
      extension = pattern.substring(1);
    } else if (pattern.endsWith("/*")) {
      prefix = pattern.substring(0, pattern.length() - 2);
    } else if (pattern.equals("/")) {
      prefix = "";
    } else {
      throw new ConfigException("Bad url-pattern: " + pattern);
    }
  }

  /**
   * Get mapping extension
   */
  public String getExtension() {
    return extension;
  }

  /**
   * Get mapping prefix
   */
  public String getPrefix() {
    return prefix;
  }

  /**
   * Answer path for specified action.
   * The action may have a query string, which is also appended to
   * the returned path.
   * @param action the module-relative action path (with optional query string)
   * @return context-relative path that selects the specified action
   */
  public String getPath(String action) {
    StringBuffer s = new StringBuffer();
    if (prefix != null) {
      s.append(prefix);
    }
    if (!action.startsWith("/")) {
      s.append("/");
    }
    if (extension != null) {
      int query = action.indexOf("?");
      if (query > 0) {
        // insert extension before query string
        s.append(action.substring(0, query));
        s.append(extension);
        s.append(action.substring(query));
      } else {
        int anchor = action.indexOf("#");
        if (anchor > 0) {
          // insert extension before anchor
          s.append(action.substring(0, anchor));         
          s.append(extension);
          s.append(action.substring(anchor));
        } else {
          s.append(action);
          s.append(extension);
        }
      }
    } else {
      s.append(action);
    }
    return s.toString();
  }

  /**
   * Get the action path from specified request.
   * The action path is the concatenation of servlet path and path info,
   * stripped by prefix and extension.
   * @param request
   * @return module-relative action path
   */
  public String getAction(HttpServletRequest request) {
    String action = getServletPath(request);
    if (prefix != null) {
      if (!action.startsWith(prefix)) {
        return null;
      }
      action = action.substring(prefix.length());
    }
    if (extension == null) {
      String info = getPathInfo(request);
      if (info != null) {
        action += info;
      }
    } else {
      if (!action.endsWith(extension)) {
        return null;
      }
      action = action.substring(0, action.length() - extension.length());
    }
    return action.length() == 0 ? "/" : action;
  }

}
TOP

Related Classes of de.odysseus.calyxo.control.base.ControlModuleMapping

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.