1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.wsrp.servlet;
21  
22  import com.liferay.portal.model.Company;
23  import com.liferay.portal.service.CompanyLocalServiceUtil;
24  import com.liferay.portal.util.PortalInstances;
25  import com.liferay.util.Encryptor;
26  
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  
30  import java.net.URL;
31  import java.net.URLConnection;
32  
33  import java.security.Key;
34  
35  import javax.servlet.http.HttpServlet;
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  import org.apache.wsrp4j.producer.util.Base64;
42  
43  /**
44   * <a href="ResourceProxyServlet.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Michael Young
47   *
48   */
49  public class ResourceProxyServlet extends HttpServlet {
50  
51      public void service(HttpServletRequest req, HttpServletResponse res) {
52          String targetUrl = req.getParameter("url");
53          String cookie = req.getParameter("cookie");
54  
55          if (targetUrl != null) {
56              try {
57                  long companyId = PortalInstances.getCompanyId(req);
58  
59                  Company company = CompanyLocalServiceUtil.getCompanyById(
60                          companyId);
61  
62                  Key key = company.getKeyObj();
63  
64                  targetUrl = Encryptor.decryptUnencodedAsString(
65                          key, Base64.decode(targetUrl));
66  
67                  URL url = new URL(targetUrl);
68                  URLConnection con = url.openConnection();
69  
70                  cookie = Encryptor.decryptUnencodedAsString(
71                          key, Base64.decode(cookie));
72  
73                  con.setRequestProperty("Cookie", cookie);
74  
75                  con.connect();
76  
77                  res.setContentType(con.getContentType());
78                  res.setContentLength(con.getContentLength());
79  
80                  InputStream in = con.getInputStream();
81                  OutputStream out = res.getOutputStream();
82  
83                  int next;
84  
85                  while ((next = in.read()) != -1) {
86                      out.write(next);
87                  }
88  
89                  out.flush();
90                  out.close();
91              }
92              catch (Exception e) {
93                  _log.warn(e);
94              }
95          }
96      }
97  
98      private static Log _log = LogFactory.getLog(ResourceProxyServlet.class);
99  
100 }