001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.jbi.mule.http;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025: import com.liferay.portal.kernel.servlet.HttpHeaders;
026: import com.liferay.portal.kernel.util.ContentTypes;
027: import com.liferay.util.servlet.ServletResponseUtil;
028: import com.liferay.util.servlet.UploadServletRequest;
029:
030: import java.io.IOException;
031:
032: import java.util.Enumeration;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: import javax.servlet.ServletConfig;
037: import javax.servlet.ServletException;
038: import javax.servlet.http.HttpServlet;
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041:
042: import org.mule.extras.client.MuleClient;
043: import org.mule.umo.UMOException;
044: import org.mule.umo.UMOMessage;
045:
046: /**
047: * <a href="MuleBindingServlet.java.html"><b><i>View Source</i></b></a>
048: *
049: * @author Charles May
050: *
051: */
052: public class MuleBindingServlet extends HttpServlet {
053:
054: public void init(ServletConfig config) throws ServletException {
055: super .init(config);
056:
057: if (_endpoint == null) {
058: _endpoint = config.getInitParameter("endpoint");
059: }
060: }
061:
062: public void service(HttpServletRequest req, HttpServletResponse res)
063: throws IOException, ServletException {
064:
065: String contentType = req.getHeader(HttpHeaders.CONTENT_TYPE);
066:
067: if (_log.isDebugEnabled()) {
068: _log.debug("Content type " + contentType);
069: }
070:
071: if ((contentType != null)
072: && (contentType
073: .startsWith(ContentTypes.MULTIPART_FORM_DATA))) {
074:
075: req = new UploadServletRequest(req);
076: }
077:
078: try {
079: MuleClient client = new MuleClient();
080:
081: Map payload = new HashMap();
082:
083: Enumeration enu = req.getParameterNames();
084:
085: while (enu.hasMoreElements()) {
086: try {
087: String name = (String) enu.nextElement();
088:
089: String value = req.getParameter(name);
090:
091: payload.put(name, value);
092: } catch (Exception e) {
093: }
094: }
095:
096: UMOMessage message = client.send(_endpoint, payload, null);
097:
098: String result = (String) message.getPayload();
099:
100: res.setContentType("text/xml");
101:
102: try {
103: ServletResponseUtil.write(res, result);
104: } catch (Exception e) {
105: if (_log.isWarnEnabled()) {
106: _log.warn(e, e);
107: }
108: }
109: } catch (UMOException ue) {
110: }
111: }
112:
113: private static Log _log = LogFactoryUtil
114: .getLog(MuleBindingServlet.class);
115:
116: private String _endpoint;
117:
118: }
|