View Javadoc
1   /*
2    * Copyright (c) 2002-2024, City of Paris
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
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.rest.service.param;
35  
36  import javax.ws.rs.WebApplicationException;
37  import javax.ws.rs.core.Response;
38  import javax.ws.rs.core.Response.Status;
39  
40  /**
41   * Provides simple param parsing. Implementations have to override {@link #parse(String)} to transform the String to a concrete V object.
42   * 
43   * @param <V>
44   *            the <i>real</i> object class
45   */
46  public abstract class AbstractParam<V>
47  {
48      private final String _strOriginalParam;
49      private final V _value;
50  
51      /**
52       * Builds this param and calls {@link #parse(String)}
53       * 
54       * @param strParam
55       *            the param to parse
56       * @throws WebApplicationException
57       *             if an exception occured, with preconfigured message {@link #getErrorMessage(String, Throwable)}
58       */
59      public AbstractParam( String strParam ) throws WebApplicationException
60      {
61          this._strOriginalParam = strParam;
62  
63          try
64          {
65              this._value = parse( strParam );
66          }
67          catch( Throwable e )
68          {
69              throw new WebApplicationException( onError( strParam, e ) );
70          }
71      }
72  
73      /**
74       * The value of the string {@link #getOriginalParam()}
75       * 
76       * @return the object value
77       */
78      public V getValue( )
79      {
80          return _value;
81      }
82  
83      /**
84       * Gets the original string param
85       * 
86       * @return the string
87       */
88      public String getOriginalParam( )
89      {
90          return _strOriginalParam;
91      }
92  
93      /**
94       *
95       * {@inheritDoc}
96       */
97      @Override
98      public String toString( )
99      {
100         return _value.toString( );
101     }
102 
103     /**
104      * Builds a reponse when an error occurs with {@link Status#BAD_REQUEST} as http status.
105      * 
106      * @param strParam
107      *            the string causing the error
108      * @param e
109      *            the exception
110      * @return th reponse
111      */
112     protected Response onError( String strParam, Throwable e )
113     {
114         return Response.status( Status.BAD_REQUEST ).entity( getErrorMessage( strParam, e ) ).build( );
115     }
116 
117     /**
118      * Gets an error message
119      * 
120      * @param strParam
121      *            string to parse
122      * @param e
123      *            error to handle
124      * @return error message
125      */
126     protected String getErrorMessage( String strParam, Throwable e )
127     {
128         return "Parameter : " + strParam + " cannot be parsed : " + e.getMessage( );
129     }
130 
131     /**
132      * Parse the string to its real value
133      * 
134      * @param strParam
135      *            the string to parse
136      * @return real object value
137      * @throws Throwable
138      *             if occurs
139      */
140     protected abstract V parse( String strParam ) throws Throwable;
141 }