001: /*
002: * Copyright 2005-2007 WSO2, Inc. (http://wso2.com)
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:
017: package org.wso2.esb.transport.util;
018:
019: import org.apache.axiom.attachments.utils.IOUtils;
020: import org.apache.axis2.context.ConfigurationContext;
021: import org.apache.axis2.deployment.DeploymentConstants;
022: import org.apache.axis2.description.AxisService;
023: import org.apache.ws.commons.schema.XmlSchema;
024: import org.wso2.esb.ServiceBusManager;
025: import org.wso2.esb.transport.HttpGetRequestProcessor;
026: import org.wso2.utils.NetworkUtils;
027: import org.wso2.utils.ServerConfiguration;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import java.io.ByteArrayOutputStream;
032: import java.io.InputStream;
033: import java.io.OutputStream;
034: import java.io.OutputStreamWriter;
035: import java.util.ArrayList;
036: import java.util.Map;
037:
038: /**
039: *
040: */
041: public class XsdProcessor implements HttpGetRequestProcessor {
042:
043: public void process(HttpServletRequest request,
044: HttpServletResponse response, ConfigurationContext configCtx)
045: throws Exception {
046: String requestURI = request.getRequestURI();
047: String serviceName = requestURI.substring(requestURI
048: .lastIndexOf("/") + 1);
049: AxisService axisService = configCtx.getAxisConfiguration()
050: .getServiceForActivation(serviceName);
051: OutputStream outputStream = response.getOutputStream();
052: String contextRoot = request.getContextPath();
053: if (axisService != null) {
054: if (!axisService.isActive()) {
055: response.setContentType("text/html");
056: outputStream
057: .write(("<h4>Service " + serviceName + " is inactive. Cannot display Schema.</h4>")
058: .getBytes());
059: outputStream.flush();
060: return;
061: }
062:
063: axisService.populateSchemaMappings();
064: Map schemaMappingtable = axisService
065: .getSchemaMappingTable();
066: String xsds = request.getParameter("xsd");
067: if (!"".equals(xsds)) {
068: response.setContentType("text/xml");
069: XmlSchema schema = (XmlSchema) schemaMappingtable
070: .get(xsds);
071: if (schema == null) {
072: int dotIndex = xsds.indexOf('.');
073: if (dotIndex > 0) {
074: String schemaKey = xsds.substring(0, dotIndex);
075: schema = (XmlSchema) schemaMappingtable
076: .get(schemaKey);
077: }
078: }
079: if (schema != null) {
080: //schema is there - pump it outs
081: schema.write(new OutputStreamWriter(outputStream,
082: "UTF8"));
083: outputStream.flush();
084: outputStream.close();
085: } else {
086: InputStream in = axisService.getClassLoader()
087: .getResourceAsStream(
088: DeploymentConstants.META_INF + "/"
089: + xsds);
090: if (in != null) {
091: outputStream.write(IOUtils
092: .getStreamAsByteArray(in));
093: outputStream.flush();
094: outputStream.close();
095: } else {
096: response
097: .sendError(HttpServletResponse.SC_NOT_FOUND);
098: }
099: }
100: return;
101: }
102:
103: ArrayList schemas = axisService.getSchema();
104: if (schemas.size() == 1) {
105: response.setContentType("text/xml");
106: // Write to the output stream
107: processSchema((XmlSchema) schemas.get(0), outputStream,
108: contextRoot, request);
109:
110: } else {
111: String idParam = request.getParameter("id");
112: if (idParam != null) {
113: XmlSchema schema = axisService.getSchema(Integer
114: .parseInt(idParam));
115: if (schema != null) {
116: response.setContentType("text/xml");
117: processSchema(schema, outputStream,
118: contextRoot, request);
119: } else {
120: response.setContentType("text/html");
121: outputStream.write("<h4>Schema not found!</h4>"
122: .getBytes());
123: }
124: } else {
125: String ipAddress = "http://"
126: + NetworkUtils.getLocalHostname()
127: + ":"
128: + ServiceBusManager.getInstance()
129: .getHttpPort();
130: String version = ServerConfiguration.getInstance()
131: .getFirstProperty("Version");
132: outputStream
133: .write(("<html><head>"
134: + "<title>WSO2 Web Services Application Server v"
135: + version + "Management Console"
136: + " - " + axisService.getName()
137: + " Service Schema</title>"
138: + "</head>" + "<body>"
139: + "<b>Schemas for "
140: + axisService.getName() + " service</b><br/><br/>")
141: .getBytes());
142: if (schemas.size() != 0) {
143: for (int i = 0; i < schemas.size(); i++) {
144: String st = "<a href=\""
145: + ipAddress
146: + RequestProcessorUtil
147: .getServiceContextPath(configCtx)
148: + "/" + axisService.getName()
149: + "?xsd&id=" + i
150: + "&annotation=true" + "\">Schema "
151: + i + "</a><br/>";
152: outputStream.write(st.getBytes());
153: }
154: } else {
155: outputStream.write("<p>No schemas found</p>"
156: .getBytes());
157: }
158: outputStream.write("</body></html>".getBytes());
159: }
160: }
161: } else { // service is null
162: response.setContentType("text/html");
163: outputStream
164: .write(("<h4>Service " + serviceName + " is not found. Cannot display Schema.</h4>")
165: .getBytes());
166: outputStream.flush();
167: }
168: }
169:
170: private void processSchema(XmlSchema schema,
171: OutputStream outputStream, String contextRoot,
172: HttpServletRequest request) {
173: ByteArrayOutputStream baos = new ByteArrayOutputStream();
174: schema.write(baos);
175: RequestProcessorUtil.writeDocument(baos, outputStream,
176: "annotated-xsd.xsl", contextRoot,
177: isXSDAnnotated(request));
178: }
179:
180: private boolean isXSDAnnotated(HttpServletRequest request) {
181: String param = request.getParameter("annotation");
182: if (param != null && param.length() != 0) {
183: if (param.equals("true")) {
184: return true;
185: }
186: }
187: return false;
188: }
189: }
|