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.portal.wsrp.servlet;
022:
023: import com.liferay.portal.model.Company;
024: import com.liferay.portal.service.CompanyLocalServiceUtil;
025: import com.liferay.portal.util.PortalInstances;
026: import com.liferay.util.Encryptor;
027:
028: import java.io.InputStream;
029: import java.io.OutputStream;
030:
031: import java.net.URL;
032: import java.net.URLConnection;
033:
034: import java.security.Key;
035:
036: import javax.servlet.http.HttpServlet;
037: import javax.servlet.http.HttpServletRequest;
038: import javax.servlet.http.HttpServletResponse;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.apache.wsrp4j.producer.util.Base64;
043:
044: /**
045: * <a href="ResourceProxyServlet.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Michael Young
048: *
049: */
050: public class ResourceProxyServlet extends HttpServlet {
051:
052: public void service(HttpServletRequest req, HttpServletResponse res) {
053: String targetUrl = req.getParameter("url");
054: String cookie = req.getParameter("cookie");
055:
056: if (targetUrl != null) {
057: try {
058: long companyId = PortalInstances.getCompanyId(req);
059:
060: Company company = CompanyLocalServiceUtil
061: .getCompanyById(companyId);
062:
063: Key key = company.getKeyObj();
064:
065: targetUrl = Encryptor.decryptRaw(key, Base64
066: .decode(targetUrl));
067:
068: URL url = new URL(targetUrl);
069: URLConnection con = url.openConnection();
070:
071: cookie = Encryptor.decryptRaw(key, Base64
072: .decode(cookie));
073:
074: con.setRequestProperty("Cookie", cookie);
075:
076: con.connect();
077:
078: res.setContentType(con.getContentType());
079: res.setContentLength(con.getContentLength());
080:
081: InputStream in = con.getInputStream();
082: OutputStream out = res.getOutputStream();
083:
084: int next;
085:
086: while ((next = in.read()) != -1) {
087: out.write(next);
088: }
089:
090: out.flush();
091: out.close();
092: } catch (Exception e) {
093: _log.warn(e);
094: }
095: }
096: }
097:
098: private static Log _log = LogFactory
099: .getLog(ResourceProxyServlet.class);
100:
101: }
|