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.repr;
    21
    22  import java.io.UnsupportedEncodingException;
    23  import java.net.URI;
    24
    25  import javax.ws.rs.core.HttpHeaders;
    26  import javax.ws.rs.core.MediaType;
    27  import javax.ws.rs.core.Response;
    28  import javax.ws.rs.core.Response.ResponseBuilder;
    29  import javax.ws.rs.core.Response.Status;
    30
    31  public class OutputFormat
    32  {
    33      private static final String UTF8 = "UTF-8";
    34      private final RepresentationFormat format;
    35      private final ExtensionInjector extensions;
    36      private final URI baseUri;
    37
    38      public OutputFormat( RepresentationFormat format, URI baseUri, ExtensionInjector extensions )
    39      {
    40          this.format = format;
    41          this.baseUri = baseUri;
    42          this.extensions = extensions;
    43      }
    44
    45      public final Response ok( Representation representation )
    46      {
    47          if ( representation.isEmpty() ) return noContent();
    48          return response( Response.ok(), representation );
    49      }
    50
    51      public final <REPR extends Representation & EntityRepresentation> Response created(
    52              REPR representation ) throws BadInputException
    53      {
    54          return response( Response.created( uri( representation ) ), representation );
    55      }
    56
    57      public final Response response( Status status, Representation representation ) throws BadInputException
    58      {
    59          return response( Response.status( status ), representation );
    60      }
    61
    62      public Response badRequest( Throwable exception )
    63      {
    64          return response( Response.status( Status.BAD_REQUEST ), new ExceptionRepresentation(
    65                  exception ) );
    66      }
    67
    68      public Response notFound( Throwable exception )
    69      {
    70          return response( Response.status( Status.NOT_FOUND ), new ExceptionRepresentation(
    71                  exception ) );
    72      }
    73
    74      public Response notFound()
    75      {
    76          return Response.status( Status.NOT_FOUND ).build();
    77      }
    78
    79      public Response conflict( Throwable exception )
    80      {
    81          return response( Response.status( Status.CONFLICT ),
    82                  new ExceptionRepresentation( exception ) );
    83      }
    84
    85      public Response serverError( Throwable exception )
    86      {
    87          return response( Response.status( Status.INTERNAL_SERVER_ERROR ),
    88                  new ExceptionRepresentation( exception ) );
    89      }
    90
    91      private URI uri( EntityRepresentation representation ) throws BadInputException
    92      {
    93          return URI.create( format( representation.selfUri() ) );
    94      }
    95
    96      protected Response response( ResponseBuilder response, Representation representation )
    97      {
    98          String entity = format( representation );
    99          byte[] entityAsBytes;
    100         try
    101         {
    102             entityAsBytes = entity.getBytes( UTF8 );
    103         } catch ( UnsupportedEncodingException e )
    104         {
    105             throw new RuntimeException( "Could not encode string as UTF-8", e );
    106         }
    107         return response.entity( entityAsBytes ).header( HttpHeaders.CONTENT_ENCODING, UTF8 ).type(
    108                 getMediaType() ).build();
    109     }
    110
    111     public MediaType getMediaType()
    112     {
    113         return format.mediaType;
    114     }
    115
    116     public String format( Representation representation )
    117     {
    118         return representation.serialize( format, baseUri, extensions );
    119     }
    120
    121     public Response noContent()
    122     {
    123         return Response.status( Status.NO_CONTENT ).build();
    124     }
    125 }