View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.releaser.modules.rest.rs;
36  
37  import fr.paris.lutece.plugins.releaser.business.Site;
38  import fr.paris.lutece.plugins.releaser.business.SiteHome;
39  import fr.paris.lutece.plugins.releaser.modules.rest.util.Constants;
40  import fr.paris.lutece.plugins.releaser.service.SiteService;
41  import fr.paris.lutece.plugins.rest.service.RestConstants;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.util.AppLogService;
44  
45  import org.apache.commons.lang.StringUtils;
46  
47  import com.fasterxml.jackson.databind.ObjectMapper;
48  
49  import javax.ws.rs.POST;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.ws.rs.Path;
52  import javax.ws.rs.Produces;
53  import javax.ws.rs.core.Context;
54  import javax.ws.rs.core.MediaType;
55  import javax.ws.rs.core.Response;
56  
57  /**
58   * SiteRest
59   */
60  @Path( RestConstants.BASE_PATH + Constants.API_PATH + Constants.SITE_PATH )
61  public class SiteRest
62  {   
63      private static final String MESSAGE_SITE_CREATED = "module.releaser.rest.message.siteCreated";
64      private static final String MESSAGE_SITE_ALREADY_EXIST = "module.releaser.rest.message.siteAlreadyExist";
65      private static final String MESSAGE_ERROR_EMPTY_JSON = "module.releaser.rest.message.emptyJson";
66      private static final String MESSAGE_ERROR_NULL_SITE = "module.releaser.rest.message.nullSite";
67  
68      @POST
69      @Path( Constants.SITE_ADD )
70      @Produces( MediaType.APPLICATION_JSON )
71      public Response addSite( String siteAttributes, @Context HttpServletRequest request )
72      {
73  		Site site = null;
74  		
75      	// Get attributes from json
76      	if ( StringUtils.isNotBlank( siteAttributes ) )
77          {
78  	        try
79  	        {	        	
80  	            // Format from JSON
81  	            ObjectMapper mapper = new ObjectMapper( );
82  	
83  	            site = mapper.readValue( siteAttributes, Site.class );
84  	            
85  	            // Check if the site already exist
86  	            if ( SiteService.IsSiteAlreadyExist( site.getName( ), site.getArtifactId( ), site.getScmUrl( ) ) )
87  	            {
88  	            	AppLogService.error( "Site releaser - Error : " +  I18nService.getLocalizedString( MESSAGE_SITE_ALREADY_EXIST, request.getLocale() ) );
89  	            	return Response.status( Response.Status.CONFLICT ).entity( I18nService.getLocalizedString( MESSAGE_SITE_ALREADY_EXIST, request.getLocale() ) ).build( );
90  	            }
91  	            	            
92  	            AppLogService.debug( "Site releaser / Add site - Received strJson : " + siteAttributes );
93  
94  	        	// Call site creating method
95  	        	return createSite( site, request );
96  	        }
97  	        catch (Exception ex) 
98  	        {
99  	        	AppLogService.error( "Site releaser - Error : " + ex.getMessage(), ex );
100             	return Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity( ex.getMessage( ) ).build( );
101 			}
102         }
103     	
104     	AppLogService.error( "Site releaser - Error : " +  I18nService.getLocalizedString( MESSAGE_ERROR_EMPTY_JSON, request.getLocale() ) );
105     	return Response.status( Response.Status.INTERNAL_SERVER_ERROR ).entity( I18nService.getLocalizedString( MESSAGE_ERROR_EMPTY_JSON, request.getLocale() )).build( );
106     	
107     }
108            
109     /**
110      * Create Site
111      * @param site a Site object
112      * @return the Site if created for the version 1
113      */
114     private Response createSite( Site site, @Context HttpServletRequest request )
115     {
116         if ( site == null ) 
117         {
118         	AppLogService.error( "Site releaser - Error : " + I18nService.getLocalizedString( MESSAGE_ERROR_NULL_SITE, request.getLocale() ) );
119         	return Response.status( Response.Status.BAD_REQUEST).entity( I18nService.getLocalizedString( MESSAGE_ERROR_NULL_SITE, request.getLocale() )).build( );
120         }        
121 
122         try 
123         {
124 			SiteHome.create( site );
125 		} 
126         catch (Exception ex) 
127         {
128         	AppLogService.error( "Site releaser - Error : " + ex.getMessage(), ex );
129         	return Response.status( Response.Status.INTERNAL_SERVER_ERROR).entity( ex.getMessage( ) ).build( );
130 		}
131         
132  		
133         AppLogService.debug( "Site releaser - Site created" );
134         return Response.status( Response.Status.OK ).entity(  I18nService.getLocalizedString( MESSAGE_SITE_CREATED, request.getLocale() )  ).build( );		
135     }    
136 }