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.transport.http;
021:
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.addressing.EndpointReference;
025: import org.apache.axis2.context.ConfigurationContext;
026: import org.apache.axis2.context.ConfigurationContextFactory;
027: import org.apache.axis2.context.MessageContext;
028: import org.apache.axis2.context.SessionContext;
029: import org.apache.axis2.description.Parameter;
030: import org.apache.axis2.description.TransportInDescription;
031: import org.apache.axis2.engine.ListenerManager;
032: import org.apache.axis2.transport.TransportListener;
033: import org.apache.axis2.transport.http.server.HttpFactory;
034: import org.apache.axis2.transport.http.server.HttpUtils;
035: import org.apache.axis2.transport.http.server.SessionManager;
036: import org.apache.axis2.transport.http.server.SimpleHttpServer;
037: import org.apache.axis2.util.OptionsParser;
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: import javax.xml.namespace.QName;
042: import java.io.File;
043: import java.io.IOException;
044: import java.net.SocketException;
045: import java.util.Iterator;
046:
047: /**
048: * This is a simple implementation of an HTTP server for processing
049: * SOAP requests via Apache's xml-axis2.
050: * It can be used with no configuration other than the port to listen on, or it can
051: * be configured in detail with an HttpFactory.
052: */
053: public class SimpleHTTPServer implements TransportListener {
054:
055: private static final Log log = LogFactory
056: .getLog(SimpleHTTPServer.class);
057:
058: /**
059: * Embedded commons http core based server
060: */
061: SimpleHttpServer embedded = null;
062: private String localAddress;
063: int port = -1;
064:
065: public static int DEFAULT_PORT = 8080;
066:
067: private String hostAddress = null;
068:
069: protected ConfigurationContext configurationContext;
070: protected HttpFactory httpFactory;
071: private SessionManager sessionManager;
072:
073: public SimpleHTTPServer() {
074: }
075:
076: /**
077: * Create a SimpleHTTPServer using default HttpFactory settings
078: */
079: public SimpleHTTPServer(ConfigurationContext configurationContext,
080: int port) throws AxisFault {
081: this (new HttpFactory(configurationContext, port));
082: }
083:
084: /**
085: * Create a configured SimpleHTTPServer
086: */
087: public SimpleHTTPServer(HttpFactory httpFactory) throws AxisFault {
088: this .httpFactory = httpFactory;
089: this .configurationContext = httpFactory
090: .getConfigurationContext();
091: this .port = httpFactory.getPort();
092: TransportInDescription httpDescription = new TransportInDescription(
093: Constants.TRANSPORT_HTTP);
094: httpDescription.setReceiver(this );
095: httpFactory.getListenerManager().addListener(httpDescription,
096: true);
097: sessionManager = new SessionManager();
098: }
099:
100: /**
101: * init method in TransportListener
102: *
103: * @param axisConf
104: * @param transprtIn
105: * @throws AxisFault
106: */
107: public void init(ConfigurationContext axisConf,
108: TransportInDescription transprtIn) throws AxisFault {
109: try {
110: this .configurationContext = axisConf;
111:
112: Parameter param = transprtIn.getParameter(PARAM_PORT);
113: if (param != null) {
114: this .port = Integer.parseInt((String) param.getValue());
115: }
116:
117: if (httpFactory == null) {
118: httpFactory = new HttpFactory(configurationContext,
119: port);
120: }
121:
122: param = transprtIn.getParameter(HOST_ADDRESS);
123: if (param != null) {
124: hostAddress = ((String) param.getValue()).trim();
125: } else {
126: hostAddress = httpFactory.getHostAddress();
127: }
128: } catch (Exception e1) {
129: throw AxisFault.makeFault(e1);
130: }
131: }
132:
133: /**
134: * Method main
135: *
136: * @param args
137: * @throws Exception
138: */
139: public static void main(String[] args) throws Exception {
140: int port = DEFAULT_PORT;
141: OptionsParser optionsParser = new OptionsParser(args);
142:
143: args = optionsParser.getRemainingArgs();
144: // first check if we should print usage
145: if ((optionsParser.isFlagSet('?') > 0)
146: || (optionsParser.isFlagSet('h') > 0) || args == null
147: || args.length == 0 || args.length > 3) {
148: printUsage();
149: }
150: String paramPort = optionsParser.isValueSet('p');
151: if (paramPort != null) {
152: port = Integer.parseInt(paramPort);
153: }
154:
155: boolean startAllTransports = "all".equals(optionsParser
156: .isValueSet('t'));
157: String repository = optionsParser.isValueSet('r');
158: if (repository == null) {
159: args = optionsParser.getRemainingArgs();
160: if (args != null && args[0] != null && !args[0].equals("")) {
161: repository = args[0];
162: } else {
163: printUsage();
164: }
165: }
166:
167: System.out.println("[SimpleHTTPServer] Starting");
168: System.out
169: .println("[SimpleHTTPServer] Using the Axis2 Repository "
170: + new File(repository).getAbsolutePath());
171: System.out.println("[SimpleHTTPServer] Listening on port "
172: + port);
173: try {
174: ConfigurationContext configctx = ConfigurationContextFactory
175: .createConfigurationContextFromFileSystem(
176: repository, null);
177: SimpleHTTPServer receiver = new SimpleHTTPServer(configctx,
178: port);
179: Runtime.getRuntime().addShutdownHook(
180: new ShutdownThread(receiver));
181: receiver.start();
182: ListenerManager listenerManager = configctx
183: .getListenerManager();
184: TransportInDescription trsIn = new TransportInDescription(
185: Constants.TRANSPORT_HTTP);
186: trsIn.setReceiver(receiver);
187: if (listenerManager == null) {
188: listenerManager = new ListenerManager();
189: listenerManager.init(configctx);
190: }
191: listenerManager.addListener(trsIn, true);
192:
193: // should all transports be started? specified as "-t all"
194: if (startAllTransports) {
195: Iterator iter = configctx.getAxisConfiguration()
196: .getTransportsIn().keySet().iterator();
197: while (iter.hasNext()) {
198: QName trp = (QName) iter.next();
199: if (!new QName(Constants.TRANSPORT_HTTP)
200: .equals(trp)) {
201: trsIn = (TransportInDescription) configctx
202: .getAxisConfiguration()
203: .getTransportsIn().get(trp);
204: listenerManager.addListener(trsIn, false);
205: }
206: }
207: }
208:
209: System.out.println("[SimpleHTTPServer] Started");
210: } catch (Throwable t) {
211: log.fatal("Error starting SimpleHTTPServer", t);
212: System.out.println("[SimpleHTTPServer] Shutting down");
213: }
214: }
215:
216: public static void printUsage() {
217: System.out
218: .println("Usage: SimpleHTTPServer [options] -r <repository>");
219: System.out.println(" Opts: -? this message");
220: System.out.println();
221: System.out
222: .println(" -p port :to listen on (default is 8080)");
223: System.out
224: .println(" -t all :to start all transports defined in the axis2 configuration");
225: System.exit(1);
226: }
227:
228: /**
229: * Start this server as a NON-daemon.
230: */
231: public void start() throws AxisFault {
232: try {
233: embedded = new SimpleHttpServer(httpFactory, port);
234: embedded.init();
235: embedded.start();
236: } catch (IOException e) {
237: log.error(e.getMessage(), e);
238: throw AxisFault.makeFault(e);
239: }
240: }
241:
242: /**
243: * Stop this server. Can be called safely if the system is already stopped,
244: * or if it was never started.
245: * This will interrupt any pending accept().
246: */
247: public void stop() {
248: System.out.println("[SimpleHTTPServer] Stop called");
249: if (embedded != null) {
250: try {
251: embedded.destroy();
252: } catch (Exception e) {
253: log.error(e.getMessage(), e);
254: }
255: }
256: }
257:
258: /**
259: * replyToEPR
260: * If the user has given host address paramter then it gets the high priority and
261: * ERP will be creatd using that
262: * N:B - hostAddress should be a complete url (http://www.myApp.com/ws)
263: *
264: * @param serviceName
265: * @param ip
266: * @return an EndpointReference
267: * @see org.apache.axis2.transport.TransportListener#getEPRForService(String,String)
268: */
269: public EndpointReference[] getEPRsForService(String serviceName,
270: String ip) throws AxisFault {
271: //if host address is present
272: if (hostAddress != null) {
273: if (embedded != null) {
274: String endpointRefernce = hostAddress;
275: if (configurationContext.getServiceContextPath()
276: .startsWith("/")) {
277: endpointRefernce = endpointRefernce
278: + configurationContext
279: .getServiceContextPath() + "/"
280: + serviceName;
281: } else {
282: endpointRefernce = endpointRefernce
283: + '/'
284: + configurationContext
285: .getServiceContextPath() + "/"
286: + serviceName;
287: }
288: return new EndpointReference[] { new EndpointReference(
289: endpointRefernce) };
290: } else {
291: throw new AxisFault(
292: "Unable to generate EPR for the transport : http");
293: }
294: }
295: //if the host address is not present
296: String ipAddress;
297: if (ip != null) {
298: ipAddress = ip;
299: } else {
300: try {
301: if (localAddress == null) {
302: localAddress = HttpUtils
303: .getIpAddress(configurationContext
304: .getAxisConfiguration());
305: }
306: if (localAddress == null) {
307: ipAddress = "127.0.0.1";
308: } else {
309: ipAddress = localAddress;
310: }
311: } catch (SocketException e) {
312: throw AxisFault.makeFault(e);
313: }
314: }
315: if (embedded != null) {
316: String endpointRefernce = "http://" + ipAddress + ":"
317: + embedded.getPort();
318: if (configurationContext.getServiceContextPath()
319: .startsWith("/")) {
320: endpointRefernce = endpointRefernce
321: + configurationContext.getServiceContextPath()
322: + "/" + serviceName;
323: } else {
324: endpointRefernce = endpointRefernce + '/'
325: + configurationContext.getServiceContextPath()
326: + "/" + serviceName;
327: }
328:
329: return new EndpointReference[] { new EndpointReference(
330: endpointRefernce) };
331: } else {
332: throw new AxisFault(
333: "Unable to generate EPR for the transport : http");
334: }
335: }
336:
337: /**
338: * Getter for httpFactory
339: */
340: public HttpFactory getHttpFactory() {
341: return httpFactory;
342: }
343:
344: /**
345: * Method getConfigurationContext
346: *
347: * @return the system context
348: */
349: public ConfigurationContext getConfigurationContext() {
350: return configurationContext;
351: }
352:
353: /**
354: * replyToEPR
355: * If the user has given host address paramter then it gets the high priority and
356: * ERP will be creatd using that
357: * N:B - hostAddress should be a complte url (http://www.myApp.com/ws)
358: *
359: * @param serviceName
360: * @param ip
361: * @return an EndpointReference
362: * @see org.apache.axis2.transport.TransportListener#getEPRForService(String,String)
363: */
364: public EndpointReference getEPRForService(String serviceName,
365: String ip) throws AxisFault {
366: return getEPRsForService(serviceName, ip)[0];
367: }
368:
369: /**
370: * Checks if this HTTP server instance is running.
371: *
372: * @return true/false
373: */
374: public boolean isRunning() {
375: if (embedded == null) {
376: return false;
377: }
378:
379: return embedded.isRunning();
380: }
381:
382: static class ShutdownThread extends Thread {
383: private SimpleHTTPServer server = null;
384:
385: public ShutdownThread(SimpleHTTPServer server) {
386: super ();
387: this .server = server;
388: }
389:
390: public void run() {
391: System.out.println("[SimpleHTTPServer] Shutting down");
392: server.stop();
393: System.out.println("[SimpleHTTPServer] Shutdown complete");
394: }
395: }
396:
397: public SessionContext getSessionContext(
398: MessageContext messageContext) {
399: String sessionKey = (String) messageContext
400: .getProperty(HTTPConstants.COOKIE_STRING);
401: return this .sessionManager.getSessionContext(sessionKey);
402: }
403:
404: public void destroy() {
405: this.configurationContext = null;
406: }
407: }
|