View Javadoc
1    /*
2    * Copyright (c) 2002-2018, Mairie de 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.accrochageff.rs;
35  
36  import fr.paris.lutece.plugins.accrochageff.business.FederationLink;
37  import fr.paris.lutece.plugins.accrochageff.business.FederationLinkHome;
38  import fr.paris.lutece.plugins.mylutece.modules.openam.service.OpenamService;
39  import fr.paris.lutece.plugins.rest.service.RestConstants;
40  import fr.paris.lutece.portal.service.security.LuteceUser;
41  import fr.paris.lutece.portal.service.security.SecurityService;
42  import fr.paris.lutece.util.json.ErrorJsonResponse;
43  import fr.paris.lutece.util.json.JsonResponse;
44  import fr.paris.lutece.util.json.JsonUtil;
45  
46  import org.apache.commons.lang.StringUtils;
47  import org.apache.log4j.Logger;
48  import java.util.List;
49  import javax.ws.rs.FormParam;
50  import javax.ws.rs.GET;
51  import javax.ws.rs.POST;
52  import javax.ws.rs.PUT;
53  import javax.servlet.http.HttpServletRequest;
54  import javax.ws.rs.DELETE;
55  import javax.ws.rs.Path;
56  import javax.ws.rs.PathParam;
57  import javax.ws.rs.Produces;
58  import javax.ws.rs.core.Context;
59  import javax.ws.rs.core.MediaType;
60  import javax.ws.rs.core.Response;
61  
62  /**
63   * FederationLinkRest
64   */
65  @Path( RestConstants.BASE_PATH + Constants.API_PATH + Constants.VERSION_PATH + Constants.FEDERATIONLINK_PATH )
66  public class FederationLinkRest
67  {
68      private static final int VERSION_1 = 1;
69      private final Logger _logger = Logger.getLogger( RestConstants.REST_LOGGER );
70      
71      /**
72       * Get FederationLink List
73       * @param nVersion the API version
74       * @return the FederationLink List
75       */
76      @GET
77      @Path( StringUtils.EMPTY )
78      @Produces( MediaType.APPLICATION_JSON )
79      public Response getFederationLinkList( @PathParam( Constants.VERSION ) Integer nVersion,@Context
80              HttpServletRequest request )
81      {
82          switch ( nVersion )
83          {
84              case VERSION_1:
85                  return getFederationLinkListV1( request);
86              default:
87                  break;
88          }
89          _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
90          return Response.status( Response.Status.NOT_FOUND )
91                  .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) )
92                  .build( );
93      }
94      
95      /**
96       * Get FederationLink List V1
97       * @return the FederationLink List for the version 1
98       */
99      private Response getFederationLinkListV1( HttpServletRequest request)
100     {
101         
102         String strToken=OpenamService.getInstance( ).getConnectionCookie( request );
103         List<FederationLink> listFederationLinks = FederationLinkHome.getFederationLinkList( strToken );
104         
105         if( listFederationLinks.isEmpty( ) )
106         {
107             return Response.status( Response.Status.NO_CONTENT )
108                 .entity( JsonUtil.buildJsonResponse( new JsonResponse( Constants.EMPTY_OBJECT ) ) )
109                 .build( );
110         }
111         return Response.status( Response.Status.OK )
112                 .entity( JsonUtil.buildJsonResponse( new JsonResponse( listFederationLinks ) ) )
113                 .build( );
114     }
115     
116   
117 
118     /**
119      * Delete FederationLink
120      * @param nVersion the API version
121      * @param id the id
122      * @return the FederationLink List if deleted
123      */
124     @DELETE
125     @Path( Constants.ID_PATH )
126     @Produces( MediaType.APPLICATION_JSON )
127     public Response deleteFederationLink(
128     @PathParam( Constants.VERSION ) Integer nVersion,
129     @PathParam( Constants.ID ) String id,HttpServletRequest request )
130     {
131         switch ( nVersion )
132         {
133             case VERSION_1:
134                 return deleteFederationLinkV1( id,request );
135             default:
136                 break;
137         }
138         _logger.error( Constants.ERROR_NOT_FOUND_VERSION );
139         return Response.status( Response.Status.NOT_FOUND )
140                 .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_VERSION ) ) )
141                 .build( );
142     }
143     
144     /**
145      * Delete FederationLink V1
146      * @param id the id
147      * @return the FederationLink List if deleted for the version 1
148      */
149     private Response deleteFederationLinkV1( String id ,HttpServletRequest request)
150     {
151         
152         
153         String strToken=OpenamService.getInstance( ).getConnectionCookie( request );
154         boolean bDelete = FederationLinkHome.deleteFederationLink( id, strToken );
155        
156         if (!bDelete  )
157         {
158             _logger.error( Constants.ERROR_NOT_FOUND_RESOURCE );
159             return Response.status( Response.Status.NOT_FOUND )
160                     .entity( JsonUtil.buildJsonResponse( new ErrorJsonResponse( Response.Status.NOT_FOUND.name( ), Constants.ERROR_NOT_FOUND_RESOURCE ) ) )
161                     .build( );
162         }
163         
164          
165         return Response.status( Response.Status.OK )
166                 .entity( JsonUtil.buildJsonResponse( new JsonResponse( Constants.EMPTY_OBJECT ) ) )
167                 .build( );
168     }
169     
170    
171 }