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;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.UnsupportedEncodingException;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.springframework.extensions.config.RemoteConfigElement.EndpointDescriptor;
29  import org.springframework.extensions.surf.exception.WebScriptsPlatformException;
30  import org.springframework.extensions.surf.util.I18NUtil;
31  import org.springframework.extensions.webscripts.connector.Connector;
32  import org.springframework.extensions.webscripts.connector.ConnectorContext;
33  import org.springframework.extensions.webscripts.connector.HttpMethod;
34  import org.springframework.extensions.webscripts.connector.Response;
35  
36  /**
37   * Describes a connector to a remote endpoint.
38   * 
39   * This is a wrapper around the true connector object and it provides
40   * Script-style interfaces for working with buffered response strings
41   * and the like.
42   * 
43   * @author muzquiano
44   * @author kevinr
45   */
46  public final class ScriptRemoteConnector
47  {
48      private static final Log logger = LogFactory.getLog(ScriptRemote.class);
49      
50      final private Connector connector;
51      final private EndpointDescriptor descriptor; 
52      
53      
54      /**
55       * Constructor
56       * 
57       * @param connector     The Connector to wrap
58       * @param descriptor    The description of the endpoint this connector is managing
59       */
60      public ScriptRemoteConnector(Connector connector, EndpointDescriptor descriptor)
61      {
62          this.connector = connector;
63          this.descriptor = descriptor;
64      }
65      
66      
67      /**
68       * Invokes a URI on the endpoint via a GET request.
69       * 
70       * @param uri the uri
71       * 
72       * @return Response object from the call {@link Response}
73       */
74      public Response call(String uri)
75      {
76          ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
77          return this.connector.call(uri, context);
78      }
79      
80      /**
81       * Invokes a GET request URI on the endpoint.
82       * 
83       * @param uri the uri
84       * 
85       * @return Response object from the call {@link Response}
86       */
87      public Response get(String uri)
88      {
89          return call(uri);
90      }
91      
92      /**
93       * Invokes a URI on a remote service, passing the supplied body as a POST request.
94       * 
95       * @param uri    Uri to call on the endpoint
96       * @param body   Body of the POST request.
97       * 
98       * @return Response object from the call {@link Response}
99       */
100     public Response post(String uri, String body)
101     {
102         ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
103         context.setMethod(HttpMethod.POST);
104         try
105         {
106             return this.connector.call(uri, context, new ByteArrayInputStream(body.getBytes("UTF-8")));
107         }
108         catch (UnsupportedEncodingException err)
109         {
110             throw new WebScriptsPlatformException("Unsupported encoding.", err);
111         }
112     }
113     
114     /**
115      * Invokes a URI on a remote service, passing the supplied body as a POST request.
116      * 
117      * @param uri    Uri to call on the endpoint
118      * @param body   Body of the POST request.
119      * @param contentType   Content mimetype of the request body
120      * 
121      * @return Response object from the call {@link Response}
122      */
123     public Response post(String uri, String body, String contentType)
124     {
125         ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
126         context.setMethod(HttpMethod.POST);
127         context.setContentType(contentType);
128         try
129         {
130             return this.connector.call(uri, context, new ByteArrayInputStream(body.getBytes("UTF-8")));
131         }
132         catch (UnsupportedEncodingException err)
133         {
134             throw new WebScriptsPlatformException("Unsupported encoding.", err);
135         }
136     }
137     
138     /**
139      * Invokes a URI on a remote service, passing the supplied body as a PUT request.
140      * 
141      * @param uri    Uri to call on the endpoint
142      * @param body   Body of the PUT request.
143      * 
144      * @return Response object from the call {@link Response}
145      */
146     public Response put(String uri, String body)
147     {
148         ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
149         context.setMethod(HttpMethod.PUT);
150         try
151         {
152             return this.connector.call(uri, context, new ByteArrayInputStream(body.getBytes("UTF-8")));
153         }
154         catch (UnsupportedEncodingException err)
155         {
156             throw new WebScriptsPlatformException("Unsupported encoding.", err);
157         }
158     }
159     
160     /**
161      * Invokes a URI on a remote service, passing the supplied body as a PUT request.
162      * 
163      * @param uri    Uri to call on the endpoint
164      * @param body   Body of the PUT request.
165      * @param contentType   Content mimetype of the request
166      * 
167      * @return Response object from the call {@link Response}
168      */
169     public Response put(String uri, String body, String contentType)
170     {
171         ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
172         context.setMethod(HttpMethod.PUT);
173         context.setContentType(contentType);
174         try
175         {
176             return this.connector.call(uri, context, new ByteArrayInputStream(body.getBytes("UTF-8")));
177         }
178         catch (UnsupportedEncodingException err)
179         {
180             throw new WebScriptsPlatformException("Unsupported encoding.", err);
181         }
182     }
183     
184     /**
185      * Invokes a URI on a remote service as DELETE request.
186      * 
187      * NOTE: the name of the method is 'del' not 'delete' so as to not
188      * interfere with JavaScript Object.delete() method.
189      * 
190      * @param uri    Uri to call on the endpoint
191      * 
192      * @return Response object from the call {@link Response}
193      */
194     public Response del(String uri)
195     {
196         ConnectorContext context = new ConnectorContext(null, buildDefaultHeaders());
197         context.setMethod(HttpMethod.DELETE);
198         return this.connector.call(uri, context);
199     }
200     
201     /**
202      * Returns the endpoint string
203      * 
204      * @return endpoint
205      */
206     public String getEndpoint()
207     {
208         return this.connector.getEndpoint();
209     }
210     
211     /**
212      * @return the endpoint descriptor object
213      */
214     public EndpointDescriptor getDescriptor()
215     {
216         return this.descriptor;
217     }
218     
219     
220     /**
221      * Helper to build a map of the default headers for script requests - we send over
222      * the current users locale so it can be respected by any appropriate REST APIs.
223      *  
224      * @return map of headers
225      */
226     private static Map<String, String> buildDefaultHeaders()
227     {
228         Map<String, String> headers = new HashMap<String, String>(1, 1.0f);
229         headers.put("Accept-Language", I18NUtil.getLocale().toString().replace('_', '-'));
230         return headers;
231     }
232 }