1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
76 if ( StringUtils.isNotBlank( siteAttributes ) )
77 {
78 try
79 {
80
81 ObjectMapper mapper = new ObjectMapper( );
82
83 site = mapper.readValue( siteAttributes, Site.class );
84
85
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
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
111
112
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 }