001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portlet.wsadmin;
020:
021: import java.io.IOException;
022: import java.util.List;
023: import java.util.Vector;
024:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortletException;
028:
029: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
030: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
031: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
032: import com.nabhinc.portlet.mvcportlet.core.Constants;
033: import com.nabhinc.util.StringUtil;
034: import com.nabhinc.ws.core.WebServiceException;
035: import com.nabhinc.ws.server.InterceptorInfo;
036: import com.nabhinc.ws.server.InterceptorManager;
037:
038: /**
039: * Interceptor mapping action processor.
040: *
041: * @author Padmanabh Dabke
042: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
043: */
044: public class ServiceInterceptorMappingActionProcessor extends
045: BaseRequestProcessor implements ActionProcessor {
046:
047: public String process(ActionRequest request,
048: ActionResponse response, ActionConfig actionConfig)
049: throws PortletException, IOException {
050: String action = request.getParameter("request_type");
051:
052: try {
053: if (action.equals("DeleteServiceInterceptorMapping")) {
054: int pos = Integer.parseInt(request.getParameter("pos"));
055: InterceptorManager
056: .removeServiceInterceptorMappingAt(pos);
057: } else if (action.equals("AddServiceInterceptorMapping")) {
058: addMapping(request, response);
059: } else if (action.equals("EditServiceInterceptorMapping")) {
060: setEditParameters(request, response);
061: } else if (action.equals("UpdateServiceInterceptorMapping")) {
062: updateMapping(request, response);
063: } else if (action.equals("UpdateGlobalInterceptorMapping")) {
064: updateGlobalMapping(request, response);
065: } else if (action.equals("EditGlobalInterceptorMapping")) {
066: setGlobalEditParameters(request, response);
067: }
068: } catch (WebServiceException ex) {
069: brpLog.error("Action " + action + " failed.", ex);
070: response
071: .setRenderParameter(
072: Constants.ERROR_MESSAGE_PARAM
073: + request.getPortletMode()
074: .toString().toLowerCase(),
075: ex.getMessage() == null ? "Failed to execute action."
076: : ex.getMessage());
077: return "failure";
078: }
079: return "success";
080: }
081:
082: private void addMapping(ActionRequest request,
083: ActionResponse response) throws WebServiceException,
084: IOException {
085: String serviceName = request.getParameter("ws_name");
086: String methodNames = request.getParameter("method_names");
087: String[] interceptors = constructInterceptorArray(
088: "interceptor", request);
089: boolean override = request.getParameter("override") != null;
090: InterceptorManager.addServiceInterceptorMapping(serviceName,
091: methodNames, interceptors, override);
092: }
093:
094: private void updateMapping(ActionRequest request,
095: ActionResponse response) throws WebServiceException,
096: IOException {
097: String serviceName = request.getParameter("ws_name");
098: String methodNames = request.getParameter("method_names");
099: String[] interceptors = constructInterceptorArray(
100: "interceptor", request);
101: boolean override = request.getParameter("override") != null;
102: int pos = Integer.parseInt(request.getParameter("pos"));
103: InterceptorManager.replaceServiceInterceptorMapping(
104: serviceName, methodNames, interceptors, override, pos);
105: }
106:
107: private void setEditParameters(ActionRequest request,
108: ActionResponse response) throws WebServiceException,
109: IOException {
110: int pos = Integer.parseInt(request.getParameter("pos"));
111: InterceptorManager.ServiceInterceptorMapping mapping = (InterceptorManager.ServiceInterceptorMapping) InterceptorManager
112: .getServiceInterceptorMappingList().get(pos);
113: response.setRenderParameter("ws_name", mapping.serviceName);
114: response.setRenderParameter("method_names", mapping.methods);
115: setInterceptorParameters("interceptor", response,
116: mapping.interceptorStr);
117: if (mapping.overrideGlobalInterceptorChain) {
118: response.setRenderParameter("override", "true");
119: }
120: }
121:
122: private void setGlobalEditParameters(ActionRequest request,
123: ActionResponse response) throws WebServiceException,
124: IOException {
125: String[] beforeArray = computeInterceptorArray(InterceptorManager
126: .getBeforeInterceptorInfoList());
127: if (beforeArray != null) {
128: for (int i = 0; i < beforeArray.length; i++) {
129: response.setRenderParameter("before" + i,
130: beforeArray[i]);
131: }
132: }
133: String[] afterArray = computeInterceptorArray(InterceptorManager
134: .getAfterInterceptorInfoList());
135: if (afterArray != null) {
136: for (int i = 0; i < afterArray.length; i++) {
137: response.setRenderParameter("after" + i, afterArray[i]);
138: }
139: }
140: }
141:
142: private void updateGlobalMapping(ActionRequest request,
143: ActionResponse response) throws WebServiceException,
144: IOException {
145: String[] beforeInt = constructInterceptorArray("before",
146: request);
147: String[] afterInt = constructInterceptorArray("after", request);
148: InterceptorManager.setGlobalInterceptors(beforeInt, afterInt);
149: }
150:
151: private void setInterceptorParameters(String prefix,
152: ActionResponse response, String intStr) {
153: if (intStr == null || intStr.equals(""))
154: return;
155: String[] intNames = StringUtil.split(intStr, ",");
156: for (int i = 0; i < intNames.length; i++) {
157: response.setRenderParameter(prefix + i, intNames[i]);
158: }
159: }
160:
161: private String[] constructInterceptorArray(String prefix,
162: ActionRequest request) {
163: Vector intList = new Vector(8);
164: String intStr = request.getParameter(prefix + 0);
165: int i = 0;
166: while (intStr != null && (!intStr.equals(""))) {
167: intList.addElement(intStr);
168: i++;
169: intStr = request.getParameter(prefix + i);
170: }
171: if (intList.size() == 0)
172: return null;
173: String[] intArray = new String[intList.size()];
174: intList.copyInto(intArray);
175: return intArray;
176: }
177:
178: /**
179: * Helper function that creates an array of interceptor names.
180: * @param intVec Interceptor list
181: * @return Configuration string
182: */
183: private String[] computeInterceptorArray(List intVec) {
184: if (intVec == null || intVec.size() == 0)
185: return null;
186: String[] intArray = new String[intVec.size()];
187:
188: for (int i = 0; i < intVec.size(); i++) {
189: InterceptorInfo intInfo = (InterceptorInfo) intVec.get(i);
190: intArray[i] = intInfo.name;
191: }
192: return intArray;
193: }
194:
195: }
|