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.formats;
21
22 import java.net.URI;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30
31 import org.neo4j.server.rest.repr.ListWriter;
32 import org.neo4j.server.rest.repr.MappingWriter;
33 import org.neo4j.server.rest.repr.MediaTypeNotSupportedException;
34 import org.neo4j.server.rest.repr.RepresentationFormat;
35
36 public class NullFormat extends RepresentationFormat
37 {
38 private final Collection<MediaType> supported;
39 private final MediaType[] requested;
40
41 public NullFormat(Collection<MediaType> supported, MediaType... requested)
42 {
43 super( null );
44 this.supported = supported;
45 this.requested = requested;
46 }
47
48 @Override
49 public Object readValue( String input )
50 {
51 if ( empty( input ) )
52 {
53 return null;
54 }
55 throw new MediaTypeNotSupportedException( Response.Status.UNSUPPORTED_MEDIA_TYPE, supported, requested );
56 }
57
58 @Override
59 public URI readUri( String input )
60 {
61 if ( empty( input ) )
62 {
63 return null;
64 }
65 throw new MediaTypeNotSupportedException( Response.Status.UNSUPPORTED_MEDIA_TYPE, supported, requested );
66 }
67
68 @Override
69 public Map<String, Object> readMap( String input )
70 {
71 if ( empty( input ) )
72 {
73 return Collections.emptyMap();
74 }
75 throw new MediaTypeNotSupportedException( Response.Status.UNSUPPORTED_MEDIA_TYPE, supported, requested );
76 }
77
78 @Override
79 public List<Object> readList( String input )
80 {
81 if ( empty( input ) )
82 {
83 return Collections.emptyList();
84 }
85 throw new MediaTypeNotSupportedException( Response.Status.UNSUPPORTED_MEDIA_TYPE, supported, requested );
86 }
87
88 private boolean empty( String input )
89 {
90 return input == null || "".equals( input.trim() );
91 }
92
93 @Override
94 protected String serializeValue( final String type, final Object value )
95 {
96 throw new MediaTypeNotSupportedException( Response.Status.NOT_ACCEPTABLE, supported, requested );
97 }
98
99 @Override
100 protected ListWriter serializeList( final String type )
101 {
102 throw new MediaTypeNotSupportedException( Response.Status.NOT_ACCEPTABLE, supported, requested );
103 }
104
105 @Override
106 protected MappingWriter serializeMapping( final String type )
107 {
108 throw new MediaTypeNotSupportedException( Response.Status.NOT_ACCEPTABLE, supported, requested );
109 }
110
111 @Override
112 protected String complete( final ListWriter serializer )
113 {
114 throw new MediaTypeNotSupportedException( Response.Status.NOT_ACCEPTABLE, supported, requested );
115 }
116
117 @Override
118 protected String complete( final MappingWriter serializer )
119 {
120 throw new MediaTypeNotSupportedException( Response.Status.NOT_ACCEPTABLE, supported, requested );
121 }
122 }