001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2006 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.servlet;
028:
029: import java.io.IOException;
030: import java.io.UnsupportedEncodingException;
031: import java.net.URLEncoder;
032: import java.net.URLDecoder;
033: import java.util.Collection;
034: import javax.servlet.ServletException;
035: import javax.servlet.http.HttpServlet;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: import org.cougaar.core.plugin.ComponentPlugin;
040: import org.cougaar.core.service.ServletService;
041: import org.cougaar.util.UnaryPredicate;
042:
043: /**
044: * @deprecated use ServletPlugin.
045: * @see org.cougaar.core.plugin.ServletPlugin
046: */
047: public abstract class BlackboardServlet extends ComponentPlugin {
048:
049: private String myPath;
050: private ServletService servletService;
051: private String encAgentName;
052:
053: public BlackboardServlet() {
054: super ();
055: }
056:
057: /**
058: * Optional subscriptions.
059: */
060: protected void setupSubscriptions() {
061: }
062:
063: protected void execute() {
064: }
065:
066: protected Collection query(UnaryPredicate pred) {
067: if (blackboard.isTransactionOpen()) {
068: return blackboard.query(pred);
069: } else {
070: blackboard.openTransaction();
071: Collection c = blackboard.query(pred);
072: blackboard.closeTransactionDontReset();
073: return c;
074: }
075: }
076:
077: protected void publishAdd(Object o) {
078: if (blackboard.isTransactionOpen()) {
079: blackboard.publishAdd(o);
080: } else {
081: blackboard.openTransaction();
082: blackboard.publishAdd(o);
083: blackboard.closeTransactionDontReset();
084: }
085: }
086:
087: protected void publishChange(Object o) {
088: publishChange(o, null);
089: }
090:
091: protected void publishChange(Object o, Collection changes) {
092: if (blackboard.isTransactionOpen()) {
093: blackboard.publishChange(o, changes);
094: } else {
095: blackboard.openTransaction();
096: blackboard.publishChange(o, changes);
097: blackboard.closeTransactionDontReset();
098: }
099: }
100:
101: protected void publishRemove(Object o) {
102: if (blackboard.isTransactionOpen()) {
103: blackboard.publishRemove(o);
104: } else {
105: blackboard.openTransaction();
106: blackboard.publishRemove(o);
107: blackboard.closeTransactionDontReset();
108: }
109: }
110:
111: /**
112: * Create our servlet, which by default calls our doGet/doPost/doPost
113: * methods.
114: * <p>
115: * A subclass can override this method if it's designed to create a separate
116: * servlet anyways.
117: */
118: protected HttpServlet createServlet() {
119: return new HttpServlet() {
120: protected void doGet(HttpServletRequest req,
121: HttpServletResponse resp) throws ServletException,
122: IOException {
123: BlackboardServlet.this .doGet(req, resp);
124: }
125:
126: protected void doPost(HttpServletRequest req,
127: HttpServletResponse resp) throws ServletException,
128: IOException {
129: BlackboardServlet.this .doPost(req, resp);
130: }
131:
132: protected void doPut(HttpServletRequest req,
133: HttpServletResponse resp) throws ServletException,
134: IOException {
135: BlackboardServlet.this .doPut(req, resp);
136: }
137: // note: we're missing some methods and "getOptions()" will be wrong
138: // This should be fine for nearly all servlets.
139: };
140: }
141:
142: /** Basic servlet methods */
143: protected void doGet(HttpServletRequest req,
144: HttpServletResponse resp) throws ServletException,
145: IOException {
146: notSupported(req, resp, "GET");
147: }
148:
149: protected void doPost(HttpServletRequest req,
150: HttpServletResponse resp) throws ServletException,
151: IOException {
152: notSupported(req, resp, "POST");
153: }
154:
155: protected void doPut(HttpServletRequest req,
156: HttpServletResponse resp) throws ServletException,
157: IOException {
158: notSupported(req, resp, "PUT");
159: }
160:
161: private void notSupported(HttpServletRequest req,
162: HttpServletResponse resp, String type) throws IOException {
163: String protocol = req.getProtocol();
164: String msg = "HTTP method " + type
165: + " is not supported by this URL";
166: if (protocol.endsWith("1.1")) {
167: resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED,
168: msg);
169: } else {
170: resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
171: }
172: }
173:
174: public void load() {
175: super .load();
176:
177: // set path
178: String path = getPath();
179: if (path == null) {
180: Collection params = getParameters();
181: if (!params.isEmpty()) {
182: path = (String) params.iterator().next();
183: }
184: }
185: if (path == null) {
186: throw new IllegalArgumentException("Missing path parameter");
187: }
188: myPath = path;
189:
190: // get encoded agent name
191: String agentName = (agentId == null ? null : agentId
192: .getAddress());
193: encAgentName = (agentName == null ? null : encode(agentName));
194:
195: // get our servlet service
196: servletService = (ServletService) getServiceBroker()
197: .getService(this , ServletService.class, null);
198: if (servletService == null) {
199: throw new RuntimeException(
200: "Unable to obtain ServletService");
201: }
202:
203: // register our servlet
204: try {
205: HttpServlet servlet = createServlet();
206: servletService.register(path, servlet);
207: } catch (Exception e) {
208: throw new RuntimeException("Unable to register " + path, e);
209: }
210: }
211:
212: public void unload() {
213: if (servletService != null) {
214: // this will automatically unregister our servlet
215: getServiceBroker().releaseService(this ,
216: ServletService.class, servletService);
217: servletService = null;
218: }
219:
220: super .unload();
221: }
222:
223: /**
224: * Get the path for the Servlet's registration.
225: * <p>
226: * Typically supplied by the component parameter, but subclasses can
227: * hard-code the path by overriding this method.
228: */
229: protected String getPath() {
230: return myPath;
231: }
232:
233: /** URL-encoded name of the local agent */
234: protected String getEncodedAgentName() {
235: return encAgentName;
236: }
237:
238: protected String encode(String s) {
239: try {
240: return URLEncoder.encode(s, "UTF-8");
241: } catch (UnsupportedEncodingException e) {
242: // should never happen
243: throw new RuntimeException("Unable to encode to UTF-8?");
244: }
245: }
246:
247: protected String decode(String s) {
248: try {
249: return URLDecoder.decode(s, "UTF-8");
250: } catch (UnsupportedEncodingException e) {
251: // should never happen
252: throw new RuntimeException("Unable to decode to UTF-8?");
253: }
254: }
255: }
|