1 /*
2 * Copyright (c) 2002-2023, 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
35 package fr.paris.lutece.plugins.utilitairesfo.utils;
36
37 import com.google.gson.Gson;
38 import com.google.gson.GsonBuilder;
39 import fr.paris.lutece.portal.service.util.AppLogService;
40 import org.apache.commons.beanutils.NestedNullException;
41 import org.apache.commons.beanutils.PropertyUtilsBean;
42 import org.springframework.web.util.UriUtils;
43
44 import java.lang.reflect.InvocationTargetException;
45 import java.lang.reflect.Type;
46 import java.nio.charset.StandardCharsets;
47 import java.util.Date;
48
49 public class DiversUtils
50 {
51
52 private static final Gson GSON_SERIALIZER = new GsonBuilder( ).registerTypeAdapter( Date.class, new ImprovedDateTypeAdapter( ) ).create( );
53
54 private DiversUtils( )
55 {
56 }
57
58 /**
59 * Introspection dans l'objet pour récupérer l'élément défini
60 * @param objet
61 * @param element chemin de l'élément à récupérer
62 * @return l'objet correspondant (null si objet null)
63 */
64 public static Object recupereElementDepuisObjet( Object objet, String element )
65 {
66 element = element.replace( "${", StringUtils.EMPTY );
67 element = element.replace( "}", StringUtils.EMPTY );
68
69 PropertyUtilsBean pub = new PropertyUtilsBean( );
70 try
71 {
72 return pub.getProperty( objet, element );
73 }
74 catch ( IllegalAccessException | InvocationTargetException | NoSuchMethodException | NestedNullException e )
75 {
76 AppLogService.debug( e );
77 AppLogService.info( "Erreur dans la recuperation de l'element element : " + e.getMessage( ) );
78 return null;
79 }
80 }
81
82 /**
83 * Désérialise puis resérialise en json l'objet entrant vers le type demandé. Remonte une exception JsonSyntaxException si le type ne correspond pas
84 * @param objetEntrant
85 * @param type
86 * @return
87 */
88 public static Object valideRecuperationObjetEntrantJson( Object objetEntrant, Type type)
89 {
90 return GSON_SERIALIZER.fromJson( GSON_SERIALIZER.toJson( objetEntrant ), type );
91 }
92
93 /**
94 * Encode l'uri
95 * @param uri
96 * @return
97 */
98 public static String encodeUriPath( String uri )
99 {
100 return UriUtils.encodePath(uri, StandardCharsets.UTF_8.toString());
101 }
102 /**
103 * Encode une partie de l'uri
104 * @param segmentUri
105 * @return
106 */
107 public static String encodeUriPathSegment( String segmentUri )
108 {
109 return UriUtils.encodePathSegment(segmentUri, StandardCharsets.UTF_8.toString());
110 }
111 }