1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }