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 javax.ws.rs.core.MediaType;
    23  import javax.ws.rs.core.Response;
    24  import java.net.URI;
    25  import java.util.Collection;
    26  import java.util.List;
    27  import java.util.Map;
    28
    29
    30  /**
    31   * This class decorates another RepresentationFormat (called inner here), and tries to use
    32   * inner to parse stuff. If it fails, it will throw an appropriate exception, and not just
    33   * blow up with an exception that leads to HTTP STATUS 500
    34   */
    35  public class DefaultFormat extends RepresentationFormat
    36  {
    37      private final RepresentationFormat inner;
    38      private final Collection<MediaType> supported;
    39      private final MediaType[] requested;
    40
    41      public DefaultFormat( RepresentationFormat inner,
    42                            Collection<MediaType> supported, MediaType... requested )
    43      {
    44          super( MediaType.APPLICATION_JSON_TYPE );
    45
    46          this.inner = inner;
    47          this.supported = supported;
    48          this.requested = requested;
    49      }
    50
    51      @Override
    52      protected String serializeValue( String type, Object value )
    53      {
    54          return inner.serializeValue( type, value );
    55      }
    56
    57      @Override
    58      protected ListWriter serializeList( String type )
    59      {
    60          return inner.serializeList( type );
    61      }
    62
    63      @Override
    64      protected MappingWriter serializeMapping( String type )
    65      {
    66          return inner.serializeMapping( type );
    67      }
    68
    69      @Override
    70      protected String complete( ListWriter serializer )
    71      {
    72          return inner.complete( serializer );
    73      }
    74
    75      @Override
    76      protected String complete( MappingWriter serializer )
    77      {
    78          return inner.complete( serializer );
    79      }
    80
    81      @Override
    82      public Object readValue( String input )
    83      {
    84          try
    85          {
    86              return inner.readValue( input );
    87          } catch ( BadInputException e )
    88          {
    89              throw newMediaTypeNotSupportedException();
    90          }
    91      }
    92
    93      private MediaTypeNotSupportedException newMediaTypeNotSupportedException()
    94      {
    95          return new MediaTypeNotSupportedException( Response.Status.UNSUPPORTED_MEDIA_TYPE, supported, requested );
    96      }
    97
    98      @Override
    99      public Map<String, Object> readMap( String input ) throws BadInputException
    100     {
    101         try
    102         {
    103             return inner.readMap( input );
    104         } catch ( BadInputException e )
    105         {
    106             throw newMediaTypeNotSupportedException();
    107         }
    108     }
    109
    110     @Override
    111     public List<Object> readList( String input ) throws BadInputException
    112     {
    113         try
    114         {
    115             return inner.readList( input );
    116         } catch ( BadInputException e )
    117         {
    118             throw newMediaTypeNotSupportedException();
    119         }
    120     }
    121
    122     @Override
    123     public URI readUri( String input ) throws BadInputException
    124     {
    125         try
    126         {
    127             return inner.readUri( input );
    128         } catch ( BadInputException e )
    129         {
    130             throw newMediaTypeNotSupportedException();
    131         }
    132     }
    133 }