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.WebApplicationException;
23  import javax.ws.rs.core.MediaType;
24  import javax.ws.rs.core.Response;
25  import java.util.Collection;
26  
27  public class MediaTypeNotSupportedException extends WebApplicationException
28  {
29      private static final long serialVersionUID = 5159216782240337940L;
30  
31      public MediaTypeNotSupportedException( Response.Status status, Collection<MediaType> supported,
32                                             MediaType... requested )
33      {
34          super( createResponse( status, message( supported, requested ) ) );
35      }
36  
37      private static Response createResponse( Response.Status status, String message )
38      {
39          return Response.status( status ).entity( message ).build();
40      }
41  
42      private static String message( Collection<MediaType> supported, MediaType[] requested )
43      {
44          StringBuilder message = new StringBuilder( "No matching representation format found.\n" );
45          if ( requested.length == 0 )
46          {
47              message.append( "No requested representation format supplied." );
48          }
49          else if ( requested.length == 1 )
50          {
51              message.append( "Request format: " ).append( requested[0] ).append( "\n" );
52          }
53          else
54          {
55              message.append( "Requested formats:\n" );
56              for ( int i = 0; i < requested.length; i++ )
57              {
58                  message.append( " " ).append( i ).append( ". " );
59                  message.append( requested[i] ).append( "\n" );
60              }
61          }
62          message.append( "Supported representation formats:" );
63          if ( supported.isEmpty() )
64          {
65              message.append( " none" );
66          }
67          else
68          {
69              for ( MediaType type : supported )
70              {
71                  message.append( "\n * " ).append( type );
72              }
73          }
74          return message.toString();
75      }
76  }