001: /*
002: * $Id: LocalServletHandler.java,v 1.25 2007/09/18 08:45:09 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.common.servlet.container;
008:
009: import java.io.File;
010: import java.io.IOException;
011: import java.util.HashMap;
012: import java.util.Map;
013: import javax.servlet.ServletException;
014: import javax.servlet.http.HttpServlet;
015:
016: import org.xins.common.Log;
017:
018: /**
019: * This class allows to invoke a XINS API without using HTTP.
020: *
021: * Example:
022: * <code>
023: * LocalServletHandler handler = LocalServletHandler.getInstance("c:\\test\\myproject.war");
024: * String xmlResult = handler.query("http://127.0.0.1:8080/myproject/?_function=MyFunction&gender=f&personLastName=Lee");
025: * </code>
026: *
027: * @version $Revision: 1.25 $ $Date: 2007/09/18 08:45:09 $
028: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
029: */
030: public class LocalServletHandler {
031:
032: /**
033: * The Servlet started by this Servlet handler.
034: */
035: private HttpServlet _apiServlet;
036:
037: /**
038: * Creates a Servlet handler that allow to invoke a Servlet without starting
039: * a HTTP server.
040: *
041: * @param warFile
042: * the location of the war file containing the Servlet, cannot be
043: * <code>null</code>.
044: *
045: * @throws ServletException
046: * if the Servlet cannot be created.
047: */
048: public LocalServletHandler(File warFile) throws ServletException {
049: initServlet(warFile);
050: }
051:
052: /**
053: * Creates a Servlet handler that allow to invoke a Servlet without starting
054: * a HTTP server.
055: *
056: * @param servletClassName
057: * The name of the servlet's class to load, cannot be <code>null</code>.
058: *
059: * @throws ServletException
060: * if the Servlet cannot be created.
061: */
062: public LocalServletHandler(String servletClassName)
063: throws ServletException {
064: initServlet(servletClassName);
065: }
066:
067: /**
068: * Initializes the Servlet.
069: *
070: * @param warFile
071: * the location of the war file, cannot be <code>null</code>.
072: *
073: * @throws ServletException
074: * if the Servlet cannot be loaded.
075: */
076: public void initServlet(File warFile) throws ServletException {
077: // create and initiliaze the Servlet
078: Log.log_1503(warFile.getPath());
079: try {
080: LocalServletConfig servletConfig = new LocalServletConfig(
081: warFile);
082: _apiServlet = (HttpServlet) Class.forName(
083: servletConfig.getServletClass()).newInstance();
084: _apiServlet.init(servletConfig);
085: } catch (ServletException exception) {
086: Log.log_1508(exception);
087: throw exception;
088: } catch (Exception exception) {
089: Log.log_1509(exception);
090: throw new ServletException(exception);
091: }
092: }
093:
094: /**
095: * Initializes the Servlet.
096: *
097: * @param servletClassName
098: * The name of the servlet's class to load, cannot be <code>null</code>.
099: *
100: * @throws ServletException
101: * if the Servlet cannot be loaded.
102: */
103: public void initServlet(String servletClassName)
104: throws ServletException {
105: // create and initiliaze the Servlet
106: //Log.log_1503(warFile.getPath());
107: try {
108: _apiServlet = (HttpServlet) Class.forName(servletClassName)
109: .newInstance();
110: _apiServlet.init();
111: } catch (ServletException exception) {
112: Log.log_1508(exception);
113: throw exception;
114: } catch (Exception exception) {
115: Log.log_1509(exception);
116: throw new ServletException(exception);
117: }
118: }
119:
120: /**
121: * Gets the Servlet.
122: *
123: * @return
124: * the created Servlet or <code>null</code> if no Servlet was created.
125: */
126: public Object getServlet() {
127: return _apiServlet;
128: }
129:
130: /**
131: * Queries the Servlet with the specified URL.
132: *
133: * @param url
134: * the url query for the request.
135: *
136: * @return
137: * the servlet response.
138: *
139: * @throws IOException
140: * If the query is not handled correctly by the servlet.
141: */
142: public XINSServletResponse query(String url) throws IOException {
143: return query("GET", url, null, new HashMap());
144: }
145:
146: /**
147: * Queries the servlet with the specified method, URL, content and HTTP
148: * headers.
149: *
150: * @param method
151: * the request method, cannot be <code>null</code>.
152: *
153: * @param url
154: * the url query for the request, if <code>null</code> then the /
155: * path is used as default with no parameters.
156: *
157: * @param data
158: * the data post for the request. <code>null</code> for HTTP GET queries.
159: *
160: * @param headers
161: * the HTTP headers passed with the query, cannot be <code>null</code>.
162: * The key and the value of the Map is String. The keys are all in
163: * uppercase.
164: *
165: * @return
166: * the servlet response.
167: *
168: * @throws IOException
169: * If the query is not handled correctly by the servlet.
170: *
171: * @since XINS 1.5.0
172: */
173: public XINSServletResponse query(String method, String url,
174: String data, Map headers) throws IOException {
175:
176: Log.log_1504(url);
177:
178: XINSServletRequest request = new XINSServletRequest(method,
179: url, data, headers);
180: XINSServletResponse response = new XINSServletResponse();
181: try {
182: _apiServlet.service(request, response);
183: } catch (ServletException ex) {
184: Log.log_1505(ex);
185: throw new IOException(ex.getMessage());
186: }
187: Log.log_1506(response.getResult(), response.getStatus());
188: return response;
189: }
190:
191: /**
192: * Disposes the Servlet and closes this Servlet handler.
193: */
194: public void close() {
195: Log.log_1507();
196: _apiServlet.destroy();
197: }
198: }
|