View Javadoc

1   /* 
2    * Copyright (c) 2011, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * @author owaeld
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       * {@inheritDoc}
71       * 
72       * @see javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable)
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       * {@inheritDoc}
86       * 
87       * @see org.apache.cxf.jaxrs.client.ResponseExceptionMapper#fromResponse(javax.ws.rs.core.Response)
88       */
89      @Override
90      public WSAgreementException fromResponse( Response r )
91      {
92          //
93          // if a specific error code exists return the associated exception
94          //
95          String errorCode = (String) r.getMetadata().getFirst( META_EC_HEADER );
96          if ( errorCode != null )
97          {
98              //
99              // parse the error code and instantiate the correct exception
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                         // return an unknown error as fallback
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                 // return an unknown error in case of a wrong error code
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 }