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.axis2.context.ConfigurationContext;
020: import org.apache.axis2.description.AxisService;
021: import org.apache.axis2.description.PolicyInclude;
022: import org.apache.axis2.util.ExternalPolicySerializer;
023: import org.apache.neethi.Policy;
024: import org.apache.neethi.PolicyReference;
025: import org.apache.neethi.PolicyRegistry;
026: import org.wso2.esb.ServiceBusManager;
027: import org.wso2.esb.transport.HttpGetRequestProcessor;
028: import org.wso2.utils.NetworkUtils;
029: import org.wso2.utils.ServerConfiguration;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import java.io.OutputStream;
034: import java.util.ArrayList;
035:
036: /**
037: *
038: */
039: public class PolicyProcessor implements HttpGetRequestProcessor {
040:
041: public void process(HttpServletRequest request,
042: HttpServletResponse response, ConfigurationContext configCtx)
043: throws Exception {
044: String requestURI = request.getRequestURI();
045: String serviceName = requestURI.substring(requestURI
046: .lastIndexOf("/") + 1);
047: AxisService axisService = configCtx.getAxisConfiguration()
048: .getServiceForActivation(serviceName);
049: OutputStream outputStream = response.getOutputStream();
050:
051: if (axisService != null) {
052: if (!axisService.isActive()) {
053: response.setContentType("text/html");
054: outputStream
055: .write(("<h4>Service " + serviceName + " is inactive. Cannot display policies.</h4>")
056: .getBytes());
057: outputStream.flush();
058: } else {
059: PolicyInclude policyInclude = axisService
060: .getPolicyInclude();
061: if (policyInclude == null) {
062: response.setContentType("text/html");
063: outputStream
064: .write("<h4>Policy element is not found!</h4>"
065: .getBytes());
066: outputStream.flush();
067: return;
068:
069: }
070: ArrayList policyElements = policyInclude
071: .getPolicyElements();
072:
073: if (policyElements == null) {
074: response.setContentType("text/html");
075: outputStream
076: .write("<h4>Policy elements not found!</h4>"
077: .getBytes());
078: outputStream.flush();
079: return;
080: }
081:
082: PolicyRegistry reg = policyInclude.getPolicyRegistry();
083:
084: ExternalPolicySerializer serializer = new ExternalPolicySerializer();
085: if (configCtx.getAxisConfiguration()
086: .getLocalPolicyAssertions() != null) {
087: serializer.setAssertionsToFilter(configCtx
088: .getAxisConfiguration()
089: .getLocalPolicyAssertions());
090: }
091:
092: if (policyElements.size() == 1) {
093: response.setContentType("text/xml");
094: if (policyElements.get(0) instanceof Policy) {
095: Policy policy = (Policy) policyElements.get(0);
096: serializer.serialize(policy, outputStream);
097: } else if (policyElements.get(0) instanceof PolicyReference) {
098: String key = ((PolicyReference) policyElements
099: .get(0)).getURI();
100: if (key.startsWith("#")) {
101: key = key.substring(key.indexOf("#") + 1);
102: }
103:
104: Policy p = reg.lookup(key);
105: if (p == null) {
106: response.setContentType("text/html");
107: outputStream
108: .write("<h4>Policy element not found!</h4>"
109: .getBytes());
110: } else {
111: serializer.serialize(p, outputStream);
112: }
113: }
114: } else {
115: // for many policies
116: String idParam = request.getParameter("id");
117: if (idParam != null) {
118: Object policyObject = policyElements
119: .get(Integer.parseInt(idParam));
120: if (policyObject != null) {
121: response.setContentType("text/xml");
122: if (policyObject instanceof Policy) {
123: Policy policy = (Policy) policyObject;
124: serializer.serialize(policy,
125: outputStream);
126: } else if (policyObject instanceof PolicyReference) {
127: String key = ((PolicyReference) policyObject)
128: .getURI();
129: if (key.startsWith("#")) {
130: key = key.substring(key
131: .indexOf("#") + 1);
132: }
133: Policy p = reg.lookup(key);
134: if (p == null) {
135: response
136: .setContentType("text/html");
137: outputStream
138: .write("<h4>Policy element not found!</h4>"
139: .getBytes());
140: } else {
141: serializer.serialize(p,
142: outputStream);
143: }
144: }
145: } else {
146: response.setContentType("text/html");
147: outputStream
148: .write("<h4>Policy not found!</h4>"
149: .getBytes());
150: }
151: } else {
152: if (policyElements.size() == 0) {
153: response.setContentType("text/html");
154: outputStream
155: .write("<h4>Policy not found!</h4>"
156: .getBytes());
157: } else {
158: String ipAddress = "http://"
159: + NetworkUtils.getLocalHostname()
160: + ":"
161: + ServiceBusManager.getInstance()
162: .getHttpPort();
163: ServerConfiguration serverCofig = ServerConfiguration
164: .getInstance();
165: outputStream
166: .write(("<html><head>"
167: + "<title>WSO2 Web Services Application Server v"
168: + serverCofig
169: .getFirstProperty("Version")
170: + " Management Console - "
171: + axisService.getName()
172: + " Service Policies</title>"
173: + "</head>" + "<body>"
174: + "<b>Policies for "
175: + axisService.getName() + " service</b><br/><br/>")
176: .getBytes());
177: if (policyElements.size() != 0) {
178: String serviceContextPath = RequestProcessorUtil
179: .getServiceContextPath(configCtx);
180: for (int i = 0; i < policyElements
181: .size(); i++) {
182: String st = "<a href=\""
183: + ipAddress
184: + serviceContextPath + "/"
185: + axisService.getName()
186: + "?policy&id=" + i
187: + "\">Policy " + i
188: + "</a><br/>";
189: outputStream.write(st.getBytes());
190: }
191: } else {
192: outputStream
193: .write("<h4>No policies found</h4>"
194: .getBytes());
195: }
196: outputStream.write("</body></html>"
197: .getBytes());
198: }
199:
200: }
201: }
202: }
203: } else { // Service is null
204: response.setContentType("text/html");
205: outputStream
206: .write(("<h4>Service " + serviceName + " not found. Cannot display policies.</h4>")
207: .getBytes());
208: outputStream.flush();
209: }
210: outputStream.flush();
211: }
212: }
|