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