com.beyondjservlet.gateway.servlet.HttpMappingRuleResolver.java Source code

Java tutorial

Introduction

Here is the source code for com.beyondjservlet.gateway.servlet.HttpMappingRuleResolver.java

Source

/**
 *  Copyright 2005-2014 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you 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.beyondjservlet.gateway.servlet;

import com.beyondj.gateway.model.HttpProxyRuleBase;
import com.beyondj.gateway.support.MappingResult;
import com.beyondj.gateway.support.MappingRuleResolver;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Takes a collection of mapping rules and builds a resolver so that we can find the mapping rules
 * for a given request and response.
 */
public class HttpMappingRuleResolver {
    private MappingRuleResolver resolver = new MappingRuleResolver();

    /* public HttpMappingResult findMappingRule(HttpServletRequest request) {
    String requestURI = request.getRequestURI();
    String contextPath = request.getContextPath();
    MappingResult answer = null;
    if (contextPath != null && contextPath.length() > 0 && !contextPath.equals("/")) {
        String requestWithoutContextPath = requestURI.substring(contextPath.length());
        answer = resolver.findMappingRule(requestWithoutContextPath);
    }
    if (answer == null) {
        // lets try the full request URI with the context path to see if that maps
        answer = resolver.findMappingRule(requestURI);
    }
        
    if (answer == null) {
        answer = findMappingRuleInternal(request);
    }
    return answer != null ? new HttpMappingResult(answer) : null;
     }*/

    public HttpMappingResult findMappingRule(HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        String contextPath = request.getParameter("__c");

        if (contextPath == null) {
            return null;
        }

        if (!contextPath.equals("/") && !contextPath.startsWith("/")) {
            contextPath = "/" + contextPath;
        }

        MappingResult answer = null;
        if (contextPath != null && contextPath.length() > 0 && !contextPath.equals("/")) {
            answer = resolver.findWebContextMappingRule(contextPath);
        }
        if (answer == null) {
            // lets try the full request URI with the context path to see if that maps
            answer = resolver.findWebContextMappingRule(requestURI);
        }
        return answer != null ? new HttpMappingResult(answer) : null;
    }

    public HttpMappingResult findMappingRule(String contextPath) {

        Validate.notEmpty(contextPath, "contextPath must not be empty");

        if (contextPath == null) {
            return null;
        }

        if (!contextPath.equals("/") && !contextPath.startsWith("/")) {
            contextPath = "/" + contextPath;
        }

        MappingResult answer = null;
        if (contextPath != null && contextPath.length() > 0 && !contextPath.equals("/")) {
            answer = resolver.findWebContextMappingRule(contextPath);
        }
        return answer != null ? new HttpMappingResult(answer) : null;
    }

    public void setMappingRules(HttpProxyRuleBase mappingRules) {
        resolver.setMappingRules(mappingRules);
    }

    public HttpProxyRuleBase getMappingRules() {
        return resolver.getMappingRules();
    }

    private static final Logger LOG = LoggerFactory.getLogger(HttpMappingRuleResolver.class);
}