View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.neo4j.server.rest;
21  
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.ws.rs.core.HttpHeaders;
28  import javax.ws.rs.core.MultivaluedMap;
29  import javax.ws.rs.core.Response;
30  
31  import com.sun.jersey.api.client.ClientResponse;
32  import com.sun.jersey.core.util.StringKeyObjectValueIgnoreCaseMultivaluedMap;
33  import com.sun.net.httpserver.Headers;
34  
35  @SuppressWarnings("restriction")
36  public class JaxRsResponse extends Response
37  {
38  
39      private ClientResponse jettyResponse;
40  
41      public JaxRsResponse( ClientResponse jettyResponse )
42      {
43          this.jettyResponse = jettyResponse;
44      }
45  
46      @Override
47      public Object getEntity()
48      {
49          return jettyResponse.getEntity( Object.class );
50      }
51  
52      @Override
53      public int getStatus()
54      {
55          return jettyResponse.getStatus();
56      }
57  
58      @Override
59      public MultivaluedMap<String, Object> getMetadata()
60      {
61          MultivaluedMap<String, Object> metadata = new StringKeyObjectValueIgnoreCaseMultivaluedMap();
62          for ( Map.Entry<String, List<String>> header : jettyResponse.getHeaders().entrySet() )
63          {
64              for ( Object value : header.getValue() )
65              {
66                  metadata.putSingle( header.getKey(), value );
67              }
68          }
69          return metadata;
70      }
71  
72      public Map<String, List<String>> getHeaders()
73      {
74          Headers headers = new Headers();
75          headers.putAll( jettyResponse.getHeaders() );
76          return headers;
77      }
78  
79      public <T> T getEntity( Class<T> asType )
80      {
81          return jettyResponse.getEntity(asType);
82      }
83  
84      public URI getLocation() throws URISyntaxException
85      {
86          return new URI(getHeaders().get( HttpHeaders.LOCATION).get(0));
87      }
88  }