001: /*
002: * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.wso2.esb.transport.tomcat;
017:
018: import org.apache.axiom.om.OMElement;
019: import org.apache.axiom.om.xpath.AXIOMXPath;
020: import org.apache.axis2.context.MessageContext;
021: import org.apache.axis2.description.TransportInDescription;
022: import org.apache.axis2.transport.http.AxisServlet;
023: import org.jaxen.SimpleNamespaceContext;
024: import org.jaxen.XPath;
025: import org.wso2.esb.ServiceBusConstants;
026: import org.wso2.esb.ServiceBusManager;
027: import org.wso2.esb.transport.HttpGetRequestProcessor;
028: import org.wso2.utils.ServerConfiguration;
029:
030: import javax.servlet.ServletConfig;
031: import javax.servlet.ServletContext;
032: import javax.servlet.ServletException;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.xml.namespace.QName;
036: import java.io.IOException;
037: import java.util.Iterator;
038: import java.util.LinkedHashMap;
039: import java.util.List;
040: import java.util.Map;
041:
042: /**
043: *
044: */
045:
046: public class ESBServlet extends AxisServlet {
047:
048: private static final long serialVersionUID = 3460108128756524161L;
049: private transient Map requestProcessors = new LinkedHashMap();
050: private static final QName ITEM_QN = new QName(
051: "http://www.wso2.org/projects/esb", "Item");
052: private static final QName CLASS_QN = new QName(
053: "http://www.wso2.org/projects/esb", "Class");
054:
055: public void init(ServletConfig config) throws ServletException {
056: ServletContext servletContext = config.getServletContext();
057: this .configContext = ServiceBusManager.getInstance()
058: .getConfigurationContext();
059: this .axisConfiguration = this .configContext
060: .getAxisConfiguration();
061: servletContext.setAttribute(this .getClass().getName(), this );
062: this .servletConfig = config;
063: populateGetRequestProcessors();
064: initParams();
065: }
066:
067: public void initContextRoot(HttpServletRequest req) {
068: this .configContext.setContextRoot("/");
069: }
070:
071: protected MessageContext createMessageContext(
072: HttpServletRequest req, HttpServletResponse resp)
073: throws IOException {
074: MessageContext msgContext = super .createMessageContext(req,
075: resp);
076: TransportInDescription adminTransIn = axisConfiguration
077: .getTransportIn(ServiceBusConstants.ADMIN_TRANSPORT);
078: if (adminTransIn != null) {
079: msgContext.setTransportIn(adminTransIn);
080: }
081: return msgContext;
082: }
083:
084: protected void doGet(HttpServletRequest httpServletRequest,
085: HttpServletResponse httpServletResponse)
086: throws ServletException, IOException {
087: String queryString = httpServletRequest.getQueryString();
088: if (queryString != null) {
089: for (Iterator iter = requestProcessors.keySet().iterator(); iter
090: .hasNext();) {
091: String item = (String) iter.next();
092: if (queryString.indexOf(item) == 0
093: && (queryString.equals(item)
094: || queryString.indexOf("&") == item
095: .length() || queryString
096: .indexOf("=") == item.length())) {
097: try {
098: ((HttpGetRequestProcessor) requestProcessors
099: .get(item)).process(httpServletRequest,
100: httpServletResponse, configContext);
101: } catch (Exception e) {
102: // todo
103: }
104: break;
105: }
106: }
107: }
108: }
109:
110: private void populateGetRequestProcessors() throws ServletException {
111: try {
112: OMElement docEle = ServerConfiguration.getInstance()
113: .getDocumentElement();
114: if (docEle != null) {
115: SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
116: nsCtx.addNamespace("esb",
117: "http://www.wso2.org/projects/esb");
118: XPath xp = new AXIOMXPath(
119: "//esb:HttpGetRequestProcessors/esb:Processor");
120: xp.setNamespaceContext(nsCtx);
121: List nodeList = xp.selectNodes(docEle);
122: for (Iterator iter = nodeList.iterator(); iter
123: .hasNext();) {
124: OMElement processorEle = (OMElement) iter.next();
125: OMElement itemEle = processorEle
126: .getFirstChildWithName(ITEM_QN);
127: if (itemEle == null) {
128: throw new ServletException(
129: "Required element, 'Item' not found!");
130: }
131: OMElement classEle = processorEle
132: .getFirstChildWithName(CLASS_QN);
133: HttpGetRequestProcessor processor;
134: if (classEle == null) {
135: throw new ServletException(
136: "Required element, 'Class' not found!");
137: } else {
138: processor = (HttpGetRequestProcessor) Class
139: .forName(classEle.getText().trim())
140: .newInstance();
141: }
142: requestProcessors.put(itemEle.getText().trim(),
143: processor);
144: }
145: }
146: } catch (Exception e) {
147: throw new ServletException(e);
148: }
149: }
150: }
|