| ResourceProxyServlet.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.wsrp.servlet;
24
25 import com.liferay.portal.model.Company;
26 import com.liferay.portal.service.CompanyLocalServiceUtil;
27 import com.liferay.portal.util.PortalInstances;
28 import com.liferay.util.Encryptor;
29
30 import java.io.InputStream;
31 import java.io.OutputStream;
32
33 import java.net.URL;
34 import java.net.URLConnection;
35
36 import java.security.Key;
37
38 import javax.servlet.http.HttpServlet;
39 import javax.servlet.http.HttpServletRequest;
40 import javax.servlet.http.HttpServletResponse;
41
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.apache.wsrp4j.producer.util.Base64;
45
46 /**
47 * <a href="ResourceProxyServlet.java.html"><b><i>View Source</i></b></a>
48 *
49 * @author Michael Young
50 *
51 */
52 public class ResourceProxyServlet extends HttpServlet {
53
54 public void service(HttpServletRequest req, HttpServletResponse res) {
55 String targetUrl = req.getParameter("url");
56 String cookie = req.getParameter("cookie");
57
58 if (targetUrl != null) {
59 try {
60 long companyId = PortalInstances.getCompanyId(req);
61
62 Company company = CompanyLocalServiceUtil.getCompanyById(
63 companyId);
64
65 Key key = company.getKeyObj();
66
67 targetUrl = Encryptor.decryptUnencodedAsString(
68 key, Base64.decode(targetUrl));
69
70 URL url = new URL(targetUrl);
71 URLConnection con = url.openConnection();
72
73 cookie = Encryptor.decryptUnencodedAsString(
74 key, Base64.decode(cookie));
75
76 con.setRequestProperty("Cookie", cookie);
77
78 con.connect();
79
80 res.setContentType(con.getContentType());
81 res.setContentLength(con.getContentLength());
82
83 InputStream in = con.getInputStream();
84 OutputStream out = res.getOutputStream();
85
86 int next;
87
88 while ((next = in.read()) != -1) {
89 out.write(next);
90 }
91
92 out.flush();
93 out.close();
94 }
95 catch (Exception e) {
96 _log.warn(e);
97 }
98 }
99 }
100
101 private static Log _log = LogFactory.getLog(ResourceProxyServlet.class);
102
103 }