001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.dispatchers;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.engine.AbstractDispatcher;
024: import org.apache.axis2.context.MessageContext;
025: import org.apache.axis2.description.AxisEndpoint;
026: import org.apache.axis2.description.AxisOperation;
027: import org.apache.axis2.description.AxisService;
028: import org.apache.axis2.description.HandlerDescription;
029: import org.apache.axis2.description.WSDL2Constants;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import java.util.Collection;
034: import java.util.Iterator;
035: import java.util.Map;
036:
037: /**
038: * Dispatches the operation based on the information from the target endpoint URL.
039: */
040: public class HTTPLocationBasedDispatcher extends AbstractDispatcher {
041:
042: public static final String NAME = "HTTPLocationBasedDispatcher";
043: private static final Log log = LogFactory
044: .getLog(HTTPLocationBasedDispatcher.class);
045:
046: /*
047: * (non-Javadoc)
048: * @see org.apache.axis2.engine.AbstractDispatcher#findOperation(org.apache.axis2.description.AxisService, org.apache.axis2.context.MessageContext)
049: */
050: public AxisOperation findOperation(AxisService service,
051: MessageContext messageContext) throws AxisFault {
052:
053: AxisService axisService = messageContext.getAxisService();
054: if (axisService != null && messageContext.getTo() != null) {
055: String uri = messageContext.getTo().getAddress();
056: String httpLocation = parseRequestURL(uri, messageContext
057: .getConfigurationContext().getServiceContextPath());
058:
059: if (httpLocation != null) {
060: AxisEndpoint axisEndpoint = (AxisEndpoint) messageContext
061: .getProperty(WSDL2Constants.ENDPOINT_LOCAL_NAME);
062: if (axisEndpoint != null) {
063: Map httpLocationTable = (Map) axisEndpoint
064: .getBinding().getProperty(
065: WSDL2Constants.HTTP_LOCATION_TABLE);
066: if (httpLocationTable != null) {
067: return getOperationFromHTTPLocation(
068: httpLocation, httpLocationTable);
069: }
070: }
071: } else {
072: log
073: .debug("Attempt to check for Operation using HTTP Location failed");
074: return null;
075: }
076: }
077: return null;
078: }
079:
080: /*
081: * (non-Javadoc)
082: * @see org.apache.axis2.engine.AbstractDispatcher#findService(org.apache.axis2.context.MessageContext)
083: */
084: public AxisService findService(MessageContext messageContext)
085: throws AxisFault {
086: // This Dispatcher does not need to resolve the service, as that is handled
087: // by the RequestURIBasedDispatcher.
088: return null;
089: }
090:
091: public void initDispatcher() {
092: init(new HandlerDescription(NAME));
093: }
094:
095: private String parseRequestURL(String path, String servicePath) {
096:
097: int index = path.lastIndexOf(servicePath);
098: String service = null;
099:
100: if (-1 != index) {
101: int serviceStart = index + servicePath.length();
102: if (path.length() > serviceStart + 1) {
103: service = path.substring(serviceStart + 1);
104: }
105: }
106:
107: if (service != null) {
108: index = service.indexOf("/");
109: if (-1 != index) {
110: service = service.substring(index);
111: } else {
112: int queryIndex = service.indexOf("?");
113: if (queryIndex != -1) {
114: service = service.substring(queryIndex);
115: }
116: }
117: }
118: return service;
119: }
120:
121: /**
122: * Given the requestPath that the request came to his method returns the corresponding axisOperation
123: *
124: * @param requestPath - Part of the request url which is the part after the service name
125: * @return AxisOperation - The corresponding AxisOperation
126: */
127: private AxisOperation getOperationFromHTTPLocation(
128: String requestPath, Map httpLocationTable) {
129:
130: Collection httpLocations = httpLocationTable.keySet();
131: Iterator iter = httpLocations.iterator();
132: while (iter.hasNext()) {
133: String location = (String) iter.next();
134: int index = requestPath.indexOf(location);
135: if (index == 0) {
136: return (AxisOperation) httpLocationTable.get(location);
137: }
138:
139: }
140: return null;
141: }
142: }
|