001: /**
002: * $Id: WSDLServlet.java,v 1.7 2006/05/12 13:26:55 mg155852 Exp $
003: * Copyright 2003 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wsrp.producer.wsdl;
014:
015: import java.util.List;
016: import java.util.ArrayList;
017: import java.util.Iterator;
018: import java.util.logging.Logger;
019: import java.util.logging.Level;
020:
021: import javax.servlet.ServletException;
022:
023: import javax.servlet.http.HttpServlet;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import java.io.PrintWriter;
028:
029: import com.iplanet.sso.SSOToken;
030:
031: import com.sun.portal.wsrp.producer.Producer;
032: import com.sun.portal.wsrp.producer.ProducerManager;
033: import com.sun.portal.wsrp.producer.ProducerException;
034: import com.sun.portal.wsrp.producer.ISConnection;
035: import com.sun.portal.log.common.PortalLogger;
036:
037: public class WSDLServlet extends HttpServlet {
038: private static final String CONTENT_TYPE_TEXT_XML = "text/xml";
039:
040: private static final int MARKUP_SERVICE_TYPE = 0;
041: private static final int SERVICEDESCRIPTION_SERVICE_TYPE = 1;
042: private static final int REGISTRATION_SERVICE_TYPE = 2;
043: private static final int PORTLETMANAGEMENT_SERVICE_TYPE = 3;
044: private static final int UNKNOWN_SERVICE_TYPE = 4;
045:
046: private static final String PRODUCER_KEY = "producerKey";
047:
048: private static final String BINDINGS_WSDL_RURL = "/wsrp/specifications/version1/wsrp_v1_bindings.wsdl";
049:
050: private static final String MARKUP_BINDING = "bind:WSRP_v1_Markup_Binding_SOAP";
051: private static final String MARKUP_NAME = "WSRPBaseService";
052: private static final String REGISTRATION_BINDING = "bind:WSRP_v1_Registration_Binding_SOAP";
053: private static final String REGISTRATION_NAME = "WSRPRegistrationService";
054: private static final String PORTLETMANAGEMENT_BINDING = "bind:WSRP_v1_PortletManagement_Binding_SOAP";
055: private static final String PORTLETMANAGEMENT_NAME = "WSRPPortletManagementService";
056: private static final String SERVICEDESCRIPTION_BINDING = "bind:WSRP_v1_ServiceDescription_Binding_SOAP";
057: private static final String SERVICEDESCRIPTION_NAME = "WSRPServiceDescriptionService";
058:
059: private static final String ROUTER_PREFIX = "/wsrp/router";
060:
061: private static final String MARKUP = "markup";
062: private static final String REGISTRATION = "registration";
063: private static final String SERVICEDESCRIPTION = "servicedescription";
064: private static final String PORTLETMANAGEMENT = "portletmanagement";
065:
066: private static final String DEFAULT_PRODUCER_KEY = "default";
067:
068: private static List SERVICES = null;
069: private static Logger logger = PortalLogger
070: .getLogger(WSDLServlet.class);
071:
072: static {
073: SERVICES = new ArrayList();
074: SERVICES.add(MARKUP);
075: SERVICES.add(SERVICEDESCRIPTION);
076: SERVICES.add(REGISTRATION);
077: SERVICES.add(PORTLETMANAGEMENT);
078: }
079:
080: protected void doGet(HttpServletRequest req, HttpServletResponse res)
081: throws ServletException, java.io.IOException {
082: res.setContentType(CONTENT_TYPE_TEXT_XML);
083:
084: String producerKey = getProducerKey(req);
085:
086: if (producerKey == null || producerKey.length() == 0) {
087: res.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
088: } else {
089: String serverName = req.getServerName();
090: int serverPort = req.getServerPort();
091: String scheme = req.getScheme();
092: String contextPath = req.getContextPath();
093:
094: String urlPrefix = scheme + "://" + serverName + ":"
095: + serverPort + contextPath;
096:
097: PrintWriter p = res.getWriter();
098:
099: String bindingsURL = urlPrefix + BINDINGS_WSDL_RURL;
100:
101: p.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
102: p
103: .println("<wsdl:definitions targetNamespace=\"urn:oasis:names:tc:wsrp:v1:wsdl\" xmlns:bind=\"urn:oasis:names:tc:wsrp:v1:bind\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\">");
104: p
105: .println("<import namespace=\"urn:oasis:names:tc:wsrp:v1:bind\" location=\""
106: + bindingsURL + "\"/>");
107: p.println("<wsdl:service name=\"WSRPService\">");
108:
109: String location = null;
110: String name = null;
111: String binding = null;
112:
113: for (Iterator i = SERVICES.iterator(); i.hasNext();) {
114: int serviceType = getServiceType((String) i.next());
115:
116: switch (serviceType) {
117: case MARKUP_SERVICE_TYPE:
118: location = urlPrefix + ROUTER_PREFIX + "/" + MARKUP
119: + "/" + producerKey;
120: name = MARKUP_NAME;
121: binding = MARKUP_BINDING;
122:
123: break;
124:
125: case SERVICEDESCRIPTION_SERVICE_TYPE:
126: location = urlPrefix + ROUTER_PREFIX + "/"
127: + SERVICEDESCRIPTION + "/" + producerKey;
128: name = SERVICEDESCRIPTION_NAME;
129: binding = SERVICEDESCRIPTION_BINDING;
130:
131: break;
132:
133: case PORTLETMANAGEMENT_SERVICE_TYPE:
134: location = urlPrefix + ROUTER_PREFIX + "/"
135: + PORTLETMANAGEMENT + "/" + producerKey;
136: name = PORTLETMANAGEMENT_NAME;
137: binding = PORTLETMANAGEMENT_BINDING;
138:
139: break;
140:
141: case REGISTRATION_SERVICE_TYPE:
142: //
143: // if registration is not required, or if
144: // in-band registration is not supported,
145: // do not expose the registration port type
146: //
147:
148: try {
149: SSOToken adminToken = ISConnection
150: .getAdminToken();
151: ProducerManager producerManager = new com.sun.portal.wsrp.producer.impl.ProducerManagerImpl(
152: req, getServletConfig()
153: .getServletContext(),
154: adminToken);
155: Producer producer = producerManager
156: .getProducer(producerKey);
157:
158: if (!producer.requiresRegistration()) {
159: continue;
160: }
161: if (!producer.inbandRegistrationSupported()) {
162: continue;
163: }
164: } catch (ProducerException pe) {
165: res.setStatus(HttpServletResponse.SC_NOT_FOUND);
166: logger.log(Level.SEVERE, "", pe);
167: return;
168: }
169:
170: // expose port type
171:
172: location = urlPrefix + ROUTER_PREFIX + "/"
173: + REGISTRATION + "/" + producerKey;
174: name = REGISTRATION_NAME;
175: binding = REGISTRATION_BINDING;
176:
177: break;
178: }
179:
180: p.println("<wsdl:port binding=\"" + binding
181: + "\" name=\"" + name + "\">");
182: p.println("<soap:address location=\"" + location
183: + "\"/>");
184: p.println("</wsdl:port>");
185: }
186:
187: p.println("</wsdl:service>");
188: p.println("</wsdl:definitions>");
189: }
190: }
191:
192: private int getServiceType(String s) {
193: int serviceType;
194:
195: if (s == null) {
196: serviceType = UNKNOWN_SERVICE_TYPE;
197: } else if (s.equalsIgnoreCase(MARKUP)) {
198: serviceType = MARKUP_SERVICE_TYPE;
199: } else if (s.equalsIgnoreCase(SERVICEDESCRIPTION)) {
200: serviceType = SERVICEDESCRIPTION_SERVICE_TYPE;
201: } else if (s.equalsIgnoreCase(REGISTRATION)) {
202: serviceType = REGISTRATION_SERVICE_TYPE;
203: } else if (s.equalsIgnoreCase(PORTLETMANAGEMENT)) {
204: serviceType = PORTLETMANAGEMENT_SERVICE_TYPE;
205: } else {
206: serviceType = UNKNOWN_SERVICE_TYPE;
207: }
208:
209: return serviceType;
210: }
211:
212: private String getProducerKey(HttpServletRequest req)
213: throws ServletException {
214: String pathinfo = req.getPathInfo();
215: String producerKey = null;
216:
217: if (pathinfo == null) {
218: producerKey = DEFAULT_PRODUCER_KEY;
219: } else {
220: producerKey = pathinfo.substring(1);
221: }
222:
223: return producerKey;
224: }
225: }
|