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.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 }