View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts.servlet.mvc;
20  
21  import java.util.Map;
22  
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpServletResponse;
25  
26  import org.springframework.extensions.surf.util.URLEncoder;
27  import org.springframework.extensions.webscripts.servlet.HTTPProxy;
28  import org.springframework.web.servlet.ModelAndView;
29  import org.springframework.web.servlet.mvc.AbstractController;
30  
31  /**
32   * HTTP Proxy Controller for Spring MVC
33   * 
34   * Provides the ability to submit a URL request via the Alfresco Server i.e.
35   * the Alfresco server acts as a proxy.
36   * 
37   * This servlet accepts:
38   * 
39   * /proxy?endpoint=<endpointUrl>[&<argName>=<argValue>]*
40   * 
41   * Where:
42   * 
43   * - endpointUrl is the URL to make a request against
44   * - argName is the name of a URL argument to append to the request
45   * - argValue is the value of URL argument
46   * 
47   * E.g.:
48   * 
49   * /proxy?endpoint=http://www.alfresco.com&arg1=value1&arg2=value2
50   * 
51   * @author davidc
52   * @author muzquiano
53   */
54  public class HTTPProxyController extends AbstractController
55  {
56      private static final String PARAM_ENDPOINT = "endpoint";
57      
58      /* (non-Javadoc)
59       * @see org.alfresco.web.framework.mvc.AbstractController#createModelAndView(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
60       */
61      public ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse res) throws Exception
62      {
63          ModelAndView mav = null;
64          
65          String endpoint = null;
66          StringBuilder args = new StringBuilder(32);
67          
68          Map<String, String[]> parameters = req.getParameterMap();
69          for (Map.Entry<String, String[]> parameter : parameters.entrySet())
70          {
71              String[] values = parameter.getValue();
72              int startIdx = 0;
73              
74              if (parameter.getKey().equals(PARAM_ENDPOINT) && values.length != 0)
75              {
76                  endpoint = values[0];
77                  startIdx++;
78              }
79              
80              for (int i = startIdx; i < values.length; i++)
81              {
82                  if (args.length() != 0)
83                  {
84                      args.append("&");
85                  }
86                  args.append(parameter.getKey()).append('=').append(URLEncoder.encode(values[i]));
87              }
88          }
89          
90          if (endpoint == null || endpoint.length() == 0)
91          {
92              throw new IllegalArgumentException("endpoint argument not specified");
93          }
94          
95          String url = endpoint + ((args.length() == 0) ? "" : "?" + args.toString());
96          HTTPProxy proxy = new HTTPProxy(url, res);
97          proxy.service();
98          
99          return mav;
100     }
101     
102     /**
103      * Construct a "proxied" URL
104      * 
105      * Note: the "proxied" URL is a relative url - it is assumed that the servlet path is /proxy
106      * 
107      * @param url  the URL to proxy
108      * @return  the "proxied" url
109      */
110     public static String createProxyUrl(String url)
111     {
112         String proxy = "/proxy";
113         if (url != null && url.length() > 0)
114         {
115             int argIndex = url.lastIndexOf("?");
116             if (argIndex == -1)
117             {
118                 proxy += "?endpoint=" + url;
119             }
120             else
121             {
122                 proxy += "?endpoint=" + url.substring(0, argIndex) + "&" + url.substring(argIndex + 1);
123             }
124         }
125         
126         return proxy;
127     }
128     
129 }
130 
131