1   /**
2    * Copyright (c) 2000-2007 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.decryptRaw(key, Base64.decode(targetUrl));
68  
69                  URL url = new URL(targetUrl);
70                  URLConnection con = url.openConnection();
71  
72                  cookie = Encryptor.decryptRaw(key, Base64.decode(cookie));
73  
74                  con.setRequestProperty("Cookie", cookie);
75  
76                  con.connect();
77  
78                  res.setContentType(con.getContentType());
79                  res.setContentLength(con.getContentLength());
80  
81                  InputStream in = con.getInputStream();
82                  OutputStream out = res.getOutputStream();
83  
84                  int next;
85  
86                  while ((next = in.read()) != -1) {
87                      out.write(next);
88                  }
89  
90                  out.flush();
91                  out.close();
92              }
93              catch (Exception e) {
94                  _log.warn(e);
95              }
96          }
97      }
98  
99      private static Log _log = LogFactory.getLog(ResourceProxyServlet.class);
100 
101 }