1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.api.rest.providers;
36
37 import java.text.MessageFormat;
38
39 import javax.ws.rs.WebApplicationException;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.Response.Status;
42 import javax.ws.rs.ext.ExceptionMapper;
43
44 import org.apache.cxf.jaxrs.client.ResponseExceptionMapper;
45 import org.ogf.graap.wsag.api.exceptions.AgreementCreationException;
46 import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
47 import org.ogf.graap.wsag.api.exceptions.CreationConstraintsViolationException;
48 import org.ogf.graap.wsag.api.exceptions.NegotiationException;
49 import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
50 import org.ogf.graap.wsag.api.exceptions.ResourceUnavailableException;
51 import org.ogf.graap.wsag.api.exceptions.ResourceUnknownException;
52 import org.ogf.graap.wsag.api.exceptions.ValidationException;
53 import org.ogf.graap.wsag.api.exceptions.WSAgreementException;
54
55
56
57
58
59 public class ServiceExceptionMapper
60 implements ExceptionMapper<WSAgreementException>, ResponseExceptionMapper<WSAgreementException>
61 {
62
63 public static final String META_EC_HEADER = "WSAG4J-ERROR-CODE";
64
65 public static final String META_EM_HEADER = "WSAG4J-ERROR-MESSAGE";
66
67 private final int errorCode = 0;
68
69
70
71
72
73
74 @Override
75 public Response toResponse( WSAgreementException exception )
76 {
77 Response response =
78 Response.status( exception.getErrorClass() ).entity( exception.getMessage() ).build();
79 response.getMetadata().add( META_EC_HEADER, exception.getErrorCode() );
80 response.getMetadata().add( META_EM_HEADER, exception.getMessage() );
81 return response;
82 }
83
84
85
86
87
88
89 @Override
90 public WSAgreementException fromResponse( Response r )
91 {
92
93
94
95 String errorCode = (String) r.getMetadata().getFirst( META_EC_HEADER );
96 if ( errorCode != null )
97 {
98
99
100
101 String errorMessage = (String) r.getMetadata().getFirst( META_EM_HEADER );
102 try
103 {
104 int code = Integer.parseInt( errorCode );
105 switch ( code )
106 {
107 case AgreementCreationException.AGREEMENT_CREATION_ERROR:
108 return new AgreementCreationException( errorMessage );
109
110 case CreationConstraintsViolationException.AGREEMENT_CONSTRAINT_VALIDATION_ERROR:
111 return new CreationConstraintsViolationException( errorMessage );
112
113 case NegotiationFactoryException.NEGOTIATION_INSTANTIATION_ERROR:
114 return new NegotiationFactoryException( errorMessage );
115
116 case NegotiationException.NEGOTIATION_ERROR:
117 return new NegotiationException( errorMessage );
118
119 case ValidationException.NEGOTIATION_VALIDATION_ERROR:
120 return new ValidationException( errorMessage );
121
122 case AgreementFactoryException.AGREEMENT_FACTORY_ERROR:
123 return new AgreementFactoryException( errorMessage );
124
125 case ResourceUnknownException.RESOURCE_UNKNOWN_ERROR:
126 return new ResourceUnknownException( errorMessage );
127
128 case ResourceUnavailableException.RESOURCE_UNAVAILABLE_ERROR:
129 return new ResourceUnavailableException( errorMessage );
130
131 default:
132
133
134
135 String message = MessageFormat.format( "Unknown server error: {0}", errorMessage );
136 throw new WebApplicationException( new Exception( message ) );
137 }
138 }
139 catch ( NumberFormatException e )
140 {
141
142
143
144 String message =
145 MessageFormat.format( "Server does not specify numeric error code: {0}", errorMessage );
146 throw new WebApplicationException( new Exception( message ) );
147 }
148 }
149 else
150 {
151 if ( r.getStatus() == Status.NOT_FOUND.getStatusCode() )
152 {
153 return new ResourceUnknownException( "requested resource not found",
154 new WebApplicationException( r ) );
155 }
156 throw new WebApplicationException( r );
157 }
158 }
159 }