1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.portletbridge.mock;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.httpclient.Header;
25  import org.apache.commons.httpclient.HttpMethodBase;
26  import org.apache.commons.httpclient.HttpStatus;
27  import org.apache.commons.httpclient.methods.GetMethod;
28  import org.portletbridge.ResourceException;
29  import org.portletbridge.portlet.HttpClientCallback;
30  import org.portletbridge.portlet.HttpClientState;
31  import org.portletbridge.portlet.HttpClientTemplate;
32  
33  /***
34   * @author JMcCrindle
35   */
36  public class MockHttpClientTemplate implements HttpClientTemplate {
37  
38      private int statusCode = HttpStatus.SC_OK;
39      private InputStream responseBody = new ByteArrayInputStream(new byte[] { -1 });
40      private String responseCharSet = "UTF-8";
41      private Map responseHeaders = new HashMap();
42      
43      /***
44       * 
45       */
46      public MockHttpClientTemplate() {
47          super();
48      }
49  
50      /* (non-Javadoc)
51       * @see org.portletbridge.portlet.HttpClientTemplate#doGet(java.net.URI, org.portletbridge.portlet.HttpClientState, org.portletbridge.portlet.HttpClientCallback)
52       */
53      public Object service(HttpMethodBase method, HttpClientState state,
54              HttpClientCallback callback) throws ResourceException {
55          try {
56              return callback.doInHttpClient(statusCode, new GetMethod(method.getURI().toString()) {
57                  public InputStream getResponseBodyAsStream() throws IOException {
58                      return responseBody;
59                  }
60  
61                  public String getResponseCharSet() {
62                      return responseCharSet;
63                  }
64                  
65                  /* (non-Javadoc)
66                   * @see org.apache.commons.httpclient.HttpMethodBase#getResponseHeader(java.lang.String)
67                   */
68                  public Header getResponseHeader(String name) {
69                      String headerValue = (String) responseHeaders.get(name);
70                      if(headerValue != null) {
71                          Header header = new Header(name, headerValue);
72                          return header;
73                      } else {
74                          return null;
75                      }
76                  }
77              });
78          } catch (Throwable e) {
79              throw new ResourceException("error.httpclient", e.getMessage(), e);
80          }
81      }
82  
83      public void setupResponseHeader(String name, String value) {
84          this.responseHeaders.put(name, value);
85      }
86  
87  }