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 package fr.paris.lutece.plugins.deployment.web;
35
36 import java.io.FileInputStream;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.util.Collection;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Map;
44
45 import javax.servlet.http.HttpServletRequest;
46
47 import net.sf.json.JSONObject;
48
49 import org.apache.commons.fileupload.FileItem;
50 import org.apache.commons.lang.StringUtils;
51 import org.springframework.util.CollectionUtils;
52
53 import fr.paris.lutece.plugins.deployment.business.Application;
54 import fr.paris.lutece.plugins.deployment.business.CommandResult;
55 import fr.paris.lutece.plugins.deployment.business.Environment;
56 import fr.paris.lutece.plugins.deployment.business.FilterDeployment;
57 import fr.paris.lutece.plugins.deployment.business.IAction;
58 import fr.paris.lutece.plugins.deployment.business.InvalidRepositoryUrlException;
59 import fr.paris.lutece.plugins.deployment.business.ManageApplicationAction;
60 import fr.paris.lutece.plugins.deployment.business.ServerApplicationInstance;
61 import fr.paris.lutece.plugins.deployment.business.vcs.SvnUser;
62 import fr.paris.lutece.plugins.deployment.business.WorkflowDeploySiteContext;
63 import fr.paris.lutece.plugins.deployment.business.vcs.AbstractVCSUser;
64 import fr.paris.lutece.plugins.deployment.service.ApplicationResourceIdService;
65 import fr.paris.lutece.plugins.deployment.service.ApplicationService;
66 import fr.paris.lutece.plugins.deployment.service.DeploymentPlugin;
67 import fr.paris.lutece.plugins.deployment.service.EnvironmentResourceIdService;
68 import fr.paris.lutece.plugins.deployment.service.IActionService;
69 import fr.paris.lutece.plugins.deployment.service.IApplicationService;
70 import fr.paris.lutece.plugins.deployment.service.IDatabaseService;
71 import fr.paris.lutece.plugins.deployment.service.IEnvironmentService;
72 import fr.paris.lutece.plugins.deployment.service.IFtpService;
73 import fr.paris.lutece.plugins.deployment.service.IServerApplicationService;
74 import fr.paris.lutece.plugins.deployment.service.IWorkflowDeploySiteService;
75 import fr.paris.lutece.plugins.deployment.service.MavenService;
76 import fr.paris.lutece.plugins.deployment.service.vcs.IVCSService;
77 import fr.paris.lutece.plugins.deployment.uploadhandler.DeploymentUploadHandler;
78 import fr.paris.lutece.plugins.deployment.util.ConstanteUtils;
79 import fr.paris.lutece.plugins.deployment.util.DeploymentUtils;
80 import fr.paris.lutece.plugins.deployment.util.RepositoryUtils;
81 import fr.paris.lutece.plugins.workflowcore.business.action.Action;
82 import fr.paris.lutece.plugins.workflowcore.business.state.State;
83 import fr.paris.lutece.portal.business.rbac.RBAC;
84 import fr.paris.lutece.portal.business.user.AdminUser;
85 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
86 import fr.paris.lutece.portal.service.i18n.I18nService;
87 import fr.paris.lutece.portal.service.message.AdminMessage;
88 import fr.paris.lutece.portal.service.message.AdminMessageService;
89 import fr.paris.lutece.portal.service.plugin.Plugin;
90 import fr.paris.lutece.portal.service.plugin.PluginService;
91 import fr.paris.lutece.portal.service.rbac.RBACService;
92 import fr.paris.lutece.portal.service.spring.SpringContextService;
93 import fr.paris.lutece.portal.service.template.AppTemplateService;
94 import fr.paris.lutece.portal.service.util.AppException;
95 import fr.paris.lutece.portal.service.util.AppLogService;
96 import fr.paris.lutece.portal.service.util.AppPathService;
97 import fr.paris.lutece.portal.service.util.AppPropertiesService;
98 import fr.paris.lutece.portal.service.workflow.WorkflowService;
99 import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
100 import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
101 import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
102 import fr.paris.lutece.util.ReferenceList;
103 import fr.paris.lutece.util.html.HtmlTemplate;
104 import fr.paris.lutece.util.html.Paginator;
105 import fr.paris.lutece.util.url.UrlItem;
106 import javax.servlet.http.HttpSession;
107
108 public class DeploymentJspBean extends PluginAdminPageJspBean
109 {
110 public static final String RIGHT_DEPLOYMENT_MANAGEMENT = "DEPLOYMENT_MANAGEMENT";
111 private static final String MESSAGE_ACCESS_DENIED = "deployment.message.error.accessDenied";
112 private IApplicationService _applicationService = SpringContextService.getBean( "deployment.ApplicationService" );
113 private IEnvironmentService _environmentService = SpringContextService.getBean( "deployment.EnvironmentService" );
114 private IServerApplicationService _serverApplicationService = SpringContextService.getBean( "deployment.ServerApplicationService" );
115
116 private IDatabaseService _databaseService = SpringContextService.getBean( "deployment.DatabaseService" );
117 private IWorkflowDeploySiteService _workflowDeploySiteService = SpringContextService.getBean( "deployment.WorkflowDeploySiteService" );
118
119 private IFtpService _ftpService = SpringContextService.getBean( "deployment.FtpService" );
120
121 private IActionService _actionService = SpringContextService.getBean( "deployment.ActionService" );
122
123 private String _strCurrentPageIndex;
124 private int _nItemsPerPage;
125 private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( ConstanteUtils.PROPERTY_NB_ITEM_PER_PAGE, 50 );
126 private Integer _nIdCurrentWorkflowDeploySiteContext;
127 private DeploymentUploadHandler _handler = SpringContextService.getBean( DeploymentUploadHandler.BEAN_NAME );
128 private FilterDeployment _filterDeployment;
129
130
131
132
133
134
135
136
137
138 @Override
139 public void init( HttpServletRequest request, String strRight ) throws AccessDeniedException
140 {
141 super.init( request, strRight );
142 if ( _filterDeployment == null )
143 {
144 _filterDeployment = new FilterDeployment( );
145 }
146 }
147
148 public String getManageApplication( HttpServletRequest request )
149 {
150 String strWorkgroup = request.getParameter( ConstanteUtils.PARAM_WORKGROUP );
151 String strSearchName = request.getParameter( ConstanteUtils.PARAM_SEARCH_NAME );
152
153 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
154 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
155
156
157
158 ReferenceList refListWorkGroups = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
159
160
161 if ( strWorkgroup != null )
162 {
163 _filterDeployment.setWorkGroup( strWorkgroup );
164 }
165 if ( strSearchName != null )
166 {
167 _filterDeployment.setSearchName( strSearchName );
168 }
169
170
171 List<Application> listApplications = _applicationService.getListApplications( _filterDeployment, getPlugin( ) );
172
173 List<ManageApplicationAction> listManageActions = SpringContextService.getBeansOfType( ManageApplicationAction.class );
174
175
176 listApplications = _applicationService.getListApplicationFilteredBySearchName( listApplications, _filterDeployment, getPlugin( ) );
177
178
179 listApplications = (List<Application>) AdminWorkgroupService.getAuthorizedCollection( listApplications, getUser( ) );
180
181 HashMap<String, Collection<ManageApplicationAction>> hashManageActions = new HashMap<String, Collection<ManageApplicationAction>>( );
182
183 for ( Application application : listApplications )
184 {
185 hashManageActions.put( Integer.toString( application.getIdApplication( ) ),
186 RBACService.getAuthorizedActionsCollection( listManageActions, application, getUser( ) ) );
187 }
188
189 HashMap model = new HashMap( );
190 Paginator paginator = new Paginator( listApplications, _nItemsPerPage, getJspManageApplication( request ), Paginator.PARAMETER_PAGE_INDEX,
191 _strCurrentPageIndex );
192
193 model.put( ConstanteUtils.MARK_APPLICATION_LIST, paginator.getPageItems( ) );
194 model.put( ConstanteUtils.MARK_PAGINATOR, paginator );
195 model.put( ConstanteUtils.MARK_NB_ITEMS_PER_PAGE, paginator.getItemsPerPage( ) );
196 model.put( ConstanteUtils.MARK_USER_WORKGROUP_REF_LIST, refListWorkGroups );
197 model.put( ConstanteUtils.MARK_USER_WORKGROUP_SELECTED, ( _filterDeployment.getWorkgroup( ) != null ) ? _filterDeployment.getWorkgroup( )
198 : ConstanteUtils.CONSTANTE_ALL );
199 model.put( ConstanteUtils.MARK_MANAGE_APPLICATION_ACTIONS, hashManageActions );
200 model.put( ConstanteUtils.MARK_SEARCH_NAME, ( _filterDeployment.getSearchName( ) != null ) ? _filterDeployment.getSearchName( ) : StringUtils.EMPTY );
201 setPageTitleProperty( ConstanteUtils.PROPERTY_MANAGE_APPLICATION_PAGE_TITLE );
202
203 model.put( ConstanteUtils.MARK_CAN_CREATE_APPLICATION,
204 RBACService.isAuthorized( Application.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, ApplicationResourceIdService.PERMISSION_CREATE, getUser( ) ) );
205 HtmlTemplate templateList = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_MANAGE_APPLICATION, getLocale( ), model );
206
207 return getAdminPage( templateList.getHtml( ) );
208 }
209
210 public String getCreateApplication( HttpServletRequest request ) throws AccessDeniedException
211 {
212
213 if ( !RBACService.isAuthorized( Application.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, ApplicationResourceIdService.PERMISSION_CREATE, getUser( ) ) )
214 {
215 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
216 }
217
218 ReferenceList refListWorkGroups = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
219
220 HashMap model = new HashMap( );
221
222 model.put( ConstanteUtils.MARK_USER_WORKGROUP_REF_LIST, refListWorkGroups );
223
224
225 setPageTitleProperty( ConstanteUtils.PROPERTY_CREATE_APPLICATION_PAGE_TITLE );
226
227 HtmlTemplate templateList = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_CREATE_APPLICATION, getLocale( ), model );
228
229 return getAdminPage( templateList.getHtml( ) );
230 }
231
232 public String getModifyApplication( HttpServletRequest request ) throws AccessDeniedException
233 {
234
235 ReferenceList refListWorkGroups = AdminWorkgroupService.getUserWorkgroups( getUser( ), getLocale( ) );
236
237 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
238
239 int nIdApplication = DeploymentUtils.getIntegerParameter( strIdApplication );
240
241 Application application = _applicationService.getApplication( nIdApplication, getPlugin( ) );
242
243 if ( !isAuthorized( application, ApplicationResourceIdService.PERMISSION_MODIFY ) )
244 {
245 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
246 }
247
248
249 HashMap model = new HashMap( );
250
251 model.put( ConstanteUtils.MARK_USER_WORKGROUP_REF_LIST, refListWorkGroups );
252
253 model.put( ConstanteUtils.MARK_APPLICATION, application );
254
255 setPageTitleProperty( ConstanteUtils.PROPERTY_MODIFY_APPLICATION_PAGE_TITLE );
256
257 HtmlTemplate templateList = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_MODIFY_APPLICATION, getLocale( ), model );
258
259 return getAdminPage( templateList.getHtml( ) );
260 }
261
262 public String getViewApplication( HttpServletRequest request ) throws AccessDeniedException
263 {
264 AdminUser adminUser = getUser( );
265 SvnUser mavenUser = DeploymentUtils.getSvnUser( adminUser.getUserId( ), getLocale( ) );
266 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
267
268 int nIdApplication = DeploymentUtils.getIntegerParameter( strIdApplication );
269
270 setPageTitleProperty( ConstanteUtils.PROPERTY_MANAGE_APPLICATION_PAGE_TITLE );
271
272 HashMap model = new HashMap( );
273
274 if ( nIdApplication == ConstanteUtils.CONSTANTE_ID_NULL )
275 {
276 return getJspManageApplication( request );
277 }
278
279 if ( mavenUser != null )
280 {
281 Application application = _applicationService.getApplication( nIdApplication, getPlugin( ) );
282
283 if ( !isAuthorized( application, ApplicationResourceIdService.PERMISSION_VIEW ) )
284 {
285 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
286 }
287
288 List<Environment> listEnvironments = _environmentService.getListEnvironments( application.getCode( ), getLocale( ) );
289
290 Collection<Environment> colEnvironmentsFilter = RBACService.getAuthorizedCollection( listEnvironments,
291 EnvironmentResourceIdService.PERMISSION_DEPLOY_ON_ENVIROMENT, getUser( ) );
292
293 HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceTomcat = _serverApplicationService.getHashServerApplicationInstance(
294 application, ConstanteUtils.CONSTANTE_SERVER_TOMCAT, getLocale( ), true, true );
295
296 HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceSql = _serverApplicationService.getHashServerApplicationInstance(
297 application, ConstanteUtils.CONSTANTE_SERVER_MYSQL, getLocale( ), true, true );
298
299 HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstancePsq = _serverApplicationService.getHashServerApplicationInstance(
300 application, ConstanteUtils.CONSTANTE_SERVER_PSQ, getLocale( ), true, true );
301
302 for ( Map.Entry<String, List<ServerApplicationInstance>> entry : hashServerApplicationInstancePsq.entrySet( ) )
303 {
304 String key = entry.getKey( );
305 List<ServerApplicationInstance> listServerPsq = entry.getValue( );
306
307 if ( hashServerApplicationInstanceSql.containsKey( entry.getKey( ) ) && hashServerApplicationInstanceSql.get( entry.getKey( ) ) != null
308 && listServerPsq != null )
309 {
310 hashServerApplicationInstanceSql.get( entry.getKey( ) ).addAll( listServerPsq );
311 }
312 else
313 {
314 hashServerApplicationInstanceSql.put( key, listServerPsq );
315
316 }
317 }
318
319 ReferenceList refListEnvironements = ReferenceList.convert( colEnvironmentsFilter, "code", "name", false );
320 model.put( ConstanteUtils.MARK_ENVIRONMENT_LIST, refListEnvironements );
321 model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_TOMCAT, hashServerApplicationInstanceTomcat );
322 model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_SQL, hashServerApplicationInstanceSql );
323
324 model.put( ConstanteUtils.MARK_APPLICATION, application );
325 }
326 else
327 {
328 String strErrorMessage = I18nService.getLocalizedString( ConstanteUtils.I18n_MESSAGE_ERROR_MAVEN_USER_IS_NOT_SET, getLocale( ) );
329 model.put( ConstanteUtils.MARK_ERROR_MESSAGE, strErrorMessage );
330 model.put( ConstanteUtils.MARK_ID_USER, getUser( ).getUserId( ) );
331 }
332
333 HtmlTemplate template = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_VIEW_APPLICATION, getLocale( ), model );
334
335 return getAdminPage( template.getHtml( ) );
336 }
337
338 public String doModifyApplication( HttpServletRequest request ) throws AccessDeniedException
339 {
340 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
341 int nIdApplication = DeploymentUtils.getIntegerParameter( strIdApplication );
342
343 if ( ( request.getParameter( ConstanteUtils.PARAM_CANCEL ) == null ) && ( nIdApplication != ConstanteUtils.CONSTANTE_ID_NULL ) )
344 {
345 Application application = _applicationService.getApplication( nIdApplication, getPlugin( ) );
346 if ( !isAuthorized( application, ApplicationResourceIdService.PERMISSION_MODIFY ) )
347 {
348 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
349 }
350
351 String strError = getApplicationData( request, application );
352
353 if ( strError != null )
354 {
355 return strError;
356 }
357
358 if ( !StringUtils.isEmpty( application.getWorkgroup( ) ) && !AdminWorkgroupService.isAuthorized( application, getUser( ) ) )
359 {
360 return getJspManageApplication( request );
361
362 }
363 _applicationService.updateApplication( application, getPlugin( ) );
364 }
365
366 return getJspManageApplication( request );
367 }
368
369 public String doCreateApplication( HttpServletRequest request ) throws AccessDeniedException
370 {
371 Application application = new Application( );
372 String strError = getApplicationData( request, application );
373
374 if ( !RBACService.isAuthorized( Application.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, ApplicationResourceIdService.PERMISSION_CREATE, getUser( ) ) )
375 {
376 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
377 }
378
379 if ( strError != null )
380 {
381 return strError;
382 }
383
384
385 if ( !StringUtils.isEmpty( application.getWorkgroup( ) ) && !AdminWorkgroupService.isAuthorized( application, getUser( ) ) )
386 {
387 return getJspManageApplication( request );
388
389 }
390
391 _applicationService.createApplication( application, getPlugin( ) );
392
393 return getJspManageApplication( request );
394 }
395
396
397
398
399
400
401
402
403 public String getConfirmRemoveApplication( HttpServletRequest request ) throws AccessDeniedException
404 {
405 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
406 int nIdApplication = DeploymentUtils.getIntegerParameter( strIdApplication );
407
408 if ( nIdApplication == ConstanteUtils.CONSTANTE_ID_NULL )
409 {
410 return getJspManageApplication( request );
411 }
412
413 Application application = _applicationService.getApplication( nIdApplication, getPlugin( ) );
414
415 if ( !isAuthorized( application, ApplicationResourceIdService.PERMISSION_DELETE ) )
416 {
417 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
418 }
419 String strMessage = ConstanteUtils.PROPERTY_MESSAGE_CONFIRM_REMOVE_APPLICATION;
420 UrlItem url = new UrlItem( ConstanteUtils.JSP_REMOVE_APPLICATION );
421 url.addParameter( ConstanteUtils.PARAM_ID_APPLICATION, nIdApplication );
422
423 return AdminMessageService.getMessageUrl( request, strMessage, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
424 }
425
426
427
428
429
430
431
432
433 public String DoRemoveApplication( HttpServletRequest request ) throws AccessDeniedException
434 {
435 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
436 int nIdApplication = DeploymentUtils.getIntegerParameter( strIdApplication );
437
438 Application application = _applicationService.getApplication( nIdApplication, getPlugin( ) );
439
440 if ( !isAuthorized( application, ApplicationResourceIdService.PERMISSION_DELETE ) )
441 {
442 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
443 }
444
445 _applicationService.deleteApplication( nIdApplication, getPlugin( ) );
446
447 return getJspManageApplication( request );
448 }
449
450 public String getFormDeployApplication( HttpServletRequest request ) throws AccessDeniedException
451 {
452 String strDeployWar = request.getParameter( ConstanteUtils.PARAM_DEPLOY_WAR );
453 String strDeploySql = request.getParameter( ConstanteUtils.PARAM_DEPLOY_SQL );
454 String strInitDatabase = request.getParameter( ConstanteUtils.PARAM_INIT_DATABASE );
455 String strInitContext = request.getParameter( ConstanteUtils.PARAM_INIT_APP_CONTEXT );
456
457 Boolean bDeployWar = !StringUtils.isEmpty( strDeployWar );
458 Boolean bDeploySql = !StringUtils.isEmpty( strDeploySql );
459 Boolean bInitDatabase = !StringUtils.isEmpty( strInitDatabase );
460 Boolean bInitContext = !StringUtils.isEmpty( strInitContext );
461
462 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
463 int nIdApplication = DeploymentUtils.getIntegerParameter( strIdApplication );
464 if ( nIdApplication == ConstanteUtils.CONSTANTE_ID_NULL )
465 {
466 return getJspManageApplication( request );
467 }
468
469 Application application = _applicationService.getApplication( nIdApplication, getPlugin( ) );
470
471 AbstractVCSUser vcsUser = DeploymentUtils.getVCSUser( request, application );
472 if ( bDeployWar )
473 {
474
475 IVCSService vcsService = DeploymentUtils.getVCSService( application.getRepoType( ) );
476 try
477 {
478 vcsService.checkAuthentication( application.getUrlRepo( ), vcsUser );
479 HttpSession session = request.getSession( true );
480 session.setAttribute( ConstanteUtils.ATTRIBUTE_VCS_USER, vcsUser );
481 }
482 catch( AppException e )
483 {
484 throw new AccessDeniedException( I18nService.getLocalizedString( ConstanteUtils.PROPERTY_MESSAGE_REPO_UNAUTHORIZED_ACCESS, getLocale( ) ) );
485 }
486 }
487
488 if ( ( bDeployWar && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_DEPLOY_APPLICATION ) )
489 || ( bDeploySql && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_DEPLOY_SCRIPT ) )
490 || ( bInitDatabase && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_INIT_DATABASE ) )
491 || ( bInitContext && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_INIT_APP_CONTEXT ) ) )
492 {
493 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
494 }
495
496 setPageTitleProperty( ConstanteUtils.PROPERTY_MANAGE_APPLICATION_PAGE_TITLE );
497
498 HashMap model = new HashMap( );
499
500 if ( vcsUser != null )
501 {
502
503 List<Environment> listEnvironments = _environmentService.getListEnvironments( application.getCode( ), getLocale( ) );
504
505 Collection<Environment> colEnvironmentsFilter = RBACService.getAuthorizedCollection( listEnvironments,
506 EnvironmentResourceIdService.PERMISSION_DEPLOY_ON_ENVIROMENT, getUser( ) );
507
508 if ( bDeployWar || bInitContext )
509 {
510 HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceTomcat = _serverApplicationService
511 .getHashServerApplicationInstance( application, ConstanteUtils.CONSTANTE_SERVER_TOMCAT, getLocale( ), true, true );
512
513 model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_TOMCAT, hashServerApplicationInstanceTomcat );
514 ReferenceList refListTagSite = DeploymentUtils.getVCSService( application.getRepoType( ) ).getTagsSite( application.getUrlRepo( ), vcsUser,
515 application.getArtifactId( ) );
516 model.put( ConstanteUtils.MARK_SITE_LIST, refListTagSite );
517 }
518 else
519 if ( bDeploySql || bInitDatabase )
520 {
521
522 ReferenceList refListUpgradeFilesList = !bInitDatabase ? DeploymentUtils.getVCSService( application.getRepoType( ) ).getUpgradesFiles(
523 application.getArtifactId( ), application.getUrlRepo( ), vcsUser ) : null;
524 if ( refListUpgradeFilesList != null )
525 {
526 refListUpgradeFilesList = DeploymentUtils.addEmptyRefenceItem( refListUpgradeFilesList );
527 }
528
529 HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceSql = _serverApplicationService
530 .getHashServerApplicationInstance( application, ConstanteUtils.CONSTANTE_SERVER_MYSQL, getLocale( ), true, true );
531
532 if ( !bInitDatabase )
533 {
534
535 HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstancePSQ = _serverApplicationService
536 .getHashServerApplicationInstance( application, ConstanteUtils.CONSTANTE_SERVER_PSQ, getLocale( ), true, true );
537
538 for ( Map.Entry<String, List<ServerApplicationInstance>> entry : hashServerApplicationInstancePSQ.entrySet( ) )
539 {
540 String key = entry.getKey( );
541 List<ServerApplicationInstance> listServerPsq = entry.getValue( );
542
543 if ( hashServerApplicationInstanceSql.containsKey( entry.getKey( ) )
544 && hashServerApplicationInstanceSql.get( entry.getKey( ) ) != null && listServerPsq != null )
545 {
546 hashServerApplicationInstanceSql.get( entry.getKey( ) ).addAll( listServerPsq );
547 }
548 else
549 {
550 hashServerApplicationInstanceSql.put( key, listServerPsq );
551
552 }
553 }
554 }
555
556 HashMap<String, List<String>> hashDatabase = !bInitDatabase ? _databaseService.getHashDatabases( application.getCode( ),
557 hashServerApplicationInstanceSql, getLocale( ) ) : null;
558
559 model.put( ConstanteUtils.MARK_SERVER_INSTANCE_MAP_SQL, hashServerApplicationInstanceSql );
560 model.put( ConstanteUtils.MARK_DATABASE_MAP, hashDatabase );
561 model.put( ConstanteUtils.MARK_UPGRADE_FILE_REF_LIST, refListUpgradeFilesList );
562
563 }
564
565 HashMap<String, List<ServerApplicationInstance>> hashServerApplicationInstanceMysql = new HashMap<String, List<ServerApplicationInstance>>( );
566
567 ReferenceList refListEnvironements = ReferenceList.convert( colEnvironmentsFilter, "code", "name", false );
568 refListEnvironements = DeploymentUtils.addEmptyRefenceItem( refListEnvironements );
569
570 model.put( ConstanteUtils.MARK_ENVIRONMENT_LIST, refListEnvironements );
571 model.put( ConstanteUtils.MARK_APPLICATION, application );
572
573 model.put( ConstanteUtils.MARK_DEPLOY_WAR, bDeployWar );
574 model.put( ConstanteUtils.MARK_DEPLOY_SQL, bDeploySql );
575 model.put( ConstanteUtils.MARK_INIT_DATABASE, bInitDatabase );
576 model.put( ConstanteUtils.MARK_INIT_APP_CONTEXT, bInitContext );
577 model.put( ConstanteUtils.MARK_INIT_APP_CONTEXT, bInitContext );
578 model.put( ConstanteUtils.MARK_HANDLER, SpringContextService.getBean( DeploymentUploadHandler.BEAN_NAME ) );
579
580 }
581 else
582 {
583 String strErrorMessage = I18nService.getLocalizedString( ConstanteUtils.I18n_MESSAGE_ERROR_MAVEN_USER_IS_NOT_SET, getLocale( ) );
584 model.put( ConstanteUtils.MARK_ERROR_MESSAGE, strErrorMessage );
585 model.put( ConstanteUtils.MARK_ID_USER, getUser( ).getUserId( ) );
586 }
587
588 HtmlTemplate template = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_FORM_INIT_DEPLOY_APPLICATION, getLocale( ), model );
589
590 return getAdminPage( template.getHtml( ) );
591 }
592
593 public String getProcessDeployApplication( HttpServletRequest request ) throws AccessDeniedException
594 {
595 if ( _nIdCurrentWorkflowDeploySiteContext != null )
596 {
597 AdminUser adminUser = getUser( );
598
599 WorkflowDeploySiteContext workflowDeploySiteContext = _workflowDeploySiteService
600 .getWorkflowDeploySiteContext( _nIdCurrentWorkflowDeploySiteContext );
601
602 int nIdWorkflowSiteDeploy = DeploymentUtils.getIdWorkflowSiteDeploy( workflowDeploySiteContext );
603 Application application = _applicationService.getApplication( workflowDeploySiteContext.getIdApplication( ), getPlugin( ) );
604 Environment environment = _environmentService.getEnvironment( workflowDeploySiteContext.getCodeEnvironement( ), getLocale( ) );
605
606 HashMap model = new HashMap( );
607
608 if ( ( workflowDeploySiteContext.isDeployWar( ) && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_DEPLOY_APPLICATION,
609 environment ) )
610 || ( workflowDeploySiteContext.isDeploySql( ) && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_DEPLOY_SCRIPT,
611 environment ) )
612 || ( workflowDeploySiteContext.isInitBdd( ) && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_INIT_DATABASE,
613 environment ) )
614 || ( workflowDeploySiteContext.isInitAppContext( ) && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_INIT_APP_CONTEXT,
615 environment ) ) )
616 {
617
618 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
619 }
620
621 if ( workflowDeploySiteContext.isDeployWar( ) || workflowDeploySiteContext.isInitAppContext( ) )
622 {
623
624 ServerApplicationInstance serverApplicationInstanceTomcat = _serverApplicationService.getServerApplicationInstance( application,
625 workflowDeploySiteContext.getCodeServerInstance( ConstanteUtils.CONSTANTE_SERVER_TOMCAT ),
626 workflowDeploySiteContext.getCodeEnvironement( ), ConstanteUtils.CONSTANTE_SERVER_TOMCAT, getLocale( ), false, false );
627 model.put( ConstanteUtils.MARK_SERVER_INSTANCE, serverApplicationInstanceTomcat );
628
629 model.put( ConstanteUtils.MARK_TAG_TO_DEPLOY, workflowDeploySiteContext.getTagToDeploy( ) );
630
631 }
632 else
633 if ( workflowDeploySiteContext.isDeploySql( ) || workflowDeploySiteContext.isInitBdd( ) )
634 {
635 String serverType = !StringUtils.isEmpty( workflowDeploySiteContext.getCodeServerInstance( ConstanteUtils.CONSTANTE_SERVER_MYSQL ) ) ? ConstanteUtils.CONSTANTE_SERVER_MYSQL
636 : ConstanteUtils.CONSTANTE_SERVER_PSQ;
637 ServerApplicationInstance serverApplicationInstanceSql = _serverApplicationService.getServerApplicationInstance( application,
638 workflowDeploySiteContext.getCodeServerInstance( serverType ), workflowDeploySiteContext.getCodeEnvironement( ), serverType,
639 getLocale( ), false, false );
640 model.put( ConstanteUtils.MARK_SERVER_INSTANCE, serverApplicationInstanceSql );
641 model.put( ConstanteUtils.MARK_SCRIPT_NAME, workflowDeploySiteContext.getScriptFileItemName( ) );
642 }
643
644
645 Collection<Action> listAction = WorkflowService.getInstance( ).getActions( workflowDeploySiteContext.getId( ),
646 WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE, nIdWorkflowSiteDeploy, adminUser );
647 State state = WorkflowService.getInstance( ).getState( workflowDeploySiteContext.getId( ), WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE,
648 nIdWorkflowSiteDeploy, ConstanteUtils.CONSTANTE_ID_NULL );
649
650 model.put( ConstanteUtils.MARK_APPLICATION, application );
651 model.put( ConstanteUtils.MARK_ENVIRONMENT, environment );
652 model.put( ConstanteUtils.MARK_STATE, state.getName( ) );
653 model.put( ConstanteUtils.MARK_ACTION_LIST, listAction );
654 model.put( ConstanteUtils.MARK_DEPLOY_WAR, workflowDeploySiteContext.isDeployWar( ) );
655 model.put( ConstanteUtils.MARK_DEPLOY_SQL, workflowDeploySiteContext.isDeploySql( ) );
656 model.put( ConstanteUtils.MARK_INIT_APP_CONTEXT, workflowDeploySiteContext.isInitAppContext( ) );
657 model.put( ConstanteUtils.MARK_INIT_DATABASE, workflowDeploySiteContext.isInitBdd( ) );
658
659 setPageTitleProperty( ConstanteUtils.PROPERTY_DEPLOY_SITE_PAGE_TITLE );
660
661 HtmlTemplate template = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_DEPLOY_APPLICATION_PROCESS, getLocale( ), model );
662
663 return getAdminPage( template.getHtml( ) );
664 }
665
666 return getManageApplication( request );
667 }
668
669
670
671
672
673
674
675
676 public String doProcessActionJSON( HttpServletRequest request )
677 {
678 String strIdAction = request.getParameter( ConstanteUtils.PARAM_ID_ACTION );
679 int nIdAction = DeploymentUtils.getIntegerParameter( strIdAction );
680
681 Collection<Action> listAction = null;
682 State state = null;
683 String strJspFormDisplay = null;
684 CommandResult result = null;
685
686 if ( ( nIdAction != ConstanteUtils.CONSTANTE_ID_NULL ) && ( _nIdCurrentWorkflowDeploySiteContext != null ) )
687 {
688 WorkflowDeploySiteContext workflowDeploySiteContext = _workflowDeploySiteService
689 .getWorkflowDeploySiteContext( _nIdCurrentWorkflowDeploySiteContext );
690
691 int nIdWorkflowSiteDeploy = DeploymentUtils.getIdWorkflowSiteDeploy( workflowDeploySiteContext );
692 result = workflowDeploySiteContext.getCommandResult( );
693
694 if ( WorkflowService.getInstance( ).isDisplayTasksForm( nIdAction, getLocale( ) ) )
695 {
696 strJspFormDisplay = getJspTasksForm( request, _nIdCurrentWorkflowDeploySiteContext, nIdAction );
697 }
698 else
699 {
700 doProcessAction( workflowDeploySiteContext, nIdAction, getPlugin( ), getLocale( ), request );
701 DeploymentUtils.stopCommandResult( workflowDeploySiteContext );
702
703 listAction = WorkflowService.getInstance( ).getActions( workflowDeploySiteContext.getId( ), WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE,
704 nIdWorkflowSiteDeploy, getUser( ) );
705 state = WorkflowService.getInstance( ).getState( workflowDeploySiteContext.getId( ), WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE,
706 nIdWorkflowSiteDeploy, ConstanteUtils.CONSTANTE_ID_NULL );
707 }
708 }
709
710 return DeploymentUtils.getJSONForWorkflowAction( strJspFormDisplay, null, result, state, listAction ).toString( );
711 }
712
713
714
715
716
717
718
719
720 public String doProcessAction( HttpServletRequest request )
721 {
722 String strIdAction = request.getParameter( ConstanteUtils.PARAM_ID_ACTION );
723 int nIdAction = DeploymentUtils.getIntegerParameter( strIdAction );
724
725 if ( ( nIdAction != ConstanteUtils.CONSTANTE_ID_NULL ) && ( _nIdCurrentWorkflowDeploySiteContext != null ) )
726 {
727 WorkflowDeploySiteContext workflowDeploySiteContext = _workflowDeploySiteService
728 .getWorkflowDeploySiteContext( _nIdCurrentWorkflowDeploySiteContext );
729
730 if ( WorkflowService.getInstance( ).isDisplayTasksForm( nIdAction, getLocale( ) ) )
731 {
732 return getJspTasksForm( request, _nIdCurrentWorkflowDeploySiteContext, nIdAction );
733 }
734
735 doProcessAction( workflowDeploySiteContext, nIdAction, getPlugin( ), getLocale( ), request );
736
737 return getJspDeployApplicationProcess( request );
738 }
739
740 return getJspManageApplication( request );
741 }
742
743
744
745
746
747
748
749
750 public String doSaveTasksFormJSON( HttpServletRequest request )
751 {
752 String strIdAction = request.getParameter( ConstanteUtils.PARAM_ID_ACTION );
753 int nIdAction = DeploymentUtils.getIntegerParameter( strIdAction );
754
755 Collection<Action> listAction = null;
756 State state = null;
757
758 CommandResult result = null;
759 String strError = null;
760
761 if ( ( nIdAction != ConstanteUtils.CONSTANTE_ID_NULL ) && ( _nIdCurrentWorkflowDeploySiteContext != null ) )
762 {
763 WorkflowDeploySiteContext workflowDeploySiteContext = _workflowDeploySiteService
764 .getWorkflowDeploySiteContext( _nIdCurrentWorkflowDeploySiteContext );
765
766 int nIdWorkflowSiteDeploy = DeploymentUtils.getIdWorkflowSiteDeploy( workflowDeploySiteContext );
767 result = workflowDeploySiteContext.getCommandResult( );
768
769 strError = doSaveTaskForm( workflowDeploySiteContext.getId( ), nIdAction, getPlugin( ), getLocale( ), request );
770
771 listAction = WorkflowService.getInstance( ).getActions( workflowDeploySiteContext.getId( ), WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE,
772 nIdWorkflowSiteDeploy, getUser( ) );
773 state = WorkflowService.getInstance( ).getState( workflowDeploySiteContext.getId( ), WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE,
774 nIdWorkflowSiteDeploy, ConstanteUtils.CONSTANTE_ID_NULL );
775 }
776
777 return DeploymentUtils.getJSONForWorkflowAction( null, strError, result, state, listAction ).toString( );
778 }
779
780
781
782
783
784
785
786
787 public String doSaveTasksForm( HttpServletRequest request )
788 {
789 WorkflowDeploySiteContext workflowDeploySiteContext = _workflowDeploySiteService.getWorkflowDeploySiteContext( _nIdCurrentWorkflowDeploySiteContext );
790 String strIdAction = request.getParameter( ConstanteUtils.PARAM_ID_ACTION );
791 int nIdAction = DeploymentUtils.getIntegerParameter( strIdAction );
792
793 if ( _nIdCurrentWorkflowDeploySiteContext != ConstanteUtils.CONSTANTE_ID_NULL )
794 {
795 if ( request.getParameter( ConstanteUtils.PARAM_CANCEL ) == null )
796 {
797 String strError = doSaveTaskForm( workflowDeploySiteContext.getId( ), nIdAction, getPlugin( ), getLocale( ), request );
798
799 if ( StringUtils.isNotBlank( strError ) )
800 {
801 return AdminMessageService.getMessageUrl( request, strError, AdminMessage.TYPE_STOP );
802 }
803 }
804
805 return getJspDeployApplicationProcess( request );
806 }
807
808 return getJspManageApplication( request );
809 }
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827 private void doProcessAction( WorkflowDeploySiteContext workflowDeploySiteContext, int nIdAction, Plugin plugin, Locale locale, HttpServletRequest request )
828 {
829
830 if ( WorkflowService.getInstance( ).canProcessAction( workflowDeploySiteContext.getId( ), WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE, nIdAction,
831 ConstanteUtils.CONSTANTE_ID_NULL, request, false ) )
832 {
833 Application application = null;
834 try
835 {
836
837 DeploymentUtils.startCommandResult( workflowDeploySiteContext );
838
839 application = _applicationService.getApplication( workflowDeploySiteContext.getIdApplication( ), plugin );
840 Environment environment = _environmentService.getEnvironment( workflowDeploySiteContext.getCodeEnvironement( ), getLocale( ) );
841
842 if ( ( workflowDeploySiteContext.isDeployWar( ) && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_DEPLOY_APPLICATION,
843 environment ) )
844 || ( workflowDeploySiteContext.isDeploySql( ) && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_DEPLOY_SCRIPT,
845 environment ) )
846 || ( workflowDeploySiteContext.isInitBdd( ) && !isAuthorized( application, ApplicationResourceIdService.PERMISSION_INIT_DATABASE,
847 environment ) )
848 || ( workflowDeploySiteContext.isInitAppContext( ) && !isAuthorized( application,
849 ApplicationResourceIdService.PERMISSION_INIT_APP_CONTEXT, environment ) ) )
850 {
851
852 throw new AccessDeniedException( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
853 }
854
855
856 DeploymentUtils.deleteLocalRepository( workflowDeploySiteContext, application );
857
858 WorkflowService.getInstance( ).doProcessAction( workflowDeploySiteContext.getId( ), WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE,
859 nIdAction, ConstanteUtils.CONSTANTE_ID_NULL, request, locale, false );
860 }
861 catch( AccessDeniedException e )
862 {
863 AppLogService.error(
864 "Error processing during deployment of application" + workflowDeploySiteContext.getIdApplication( ) + "' - cause : " + e.getMessage( ),
865 e );
866 }
867 catch( Exception e )
868 {
869 AppLogService.error( "Error processing during deployment of application '" + workflowDeploySiteContext.getIdApplication( ) + "' - cause : "
870 + e.getMessage( ), e );
871 }
872 finally
873 {
874 DeploymentUtils.stopCommandResult( workflowDeploySiteContext );
875 if ( application != null )
876 {
877 DeploymentUtils.deleteLocalRepository( workflowDeploySiteContext, application );
878 }
879 }
880 }
881 }
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900 private String doSaveTaskForm( int nIdWorkflowSiteContext, int nIdAction, Plugin plugin, Locale locale, HttpServletRequest request )
901 {
902 if ( WorkflowService.getInstance( ).canProcessAction( nIdWorkflowSiteContext, WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE, nIdAction,
903 ConstanteUtils.CONSTANTE_ID_NULL, request, false ) )
904 {
905 boolean bHasSucceed = false;
906
907 try
908 {
909 String strError = WorkflowService.getInstance( ).doSaveTasksForm( nIdWorkflowSiteContext, WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE,
910 nIdAction, ConstanteUtils.CONSTANTE_ID_NULL, request, locale );
911
912 if ( strError != null )
913 {
914 return strError;
915 }
916
917 bHasSucceed = true;
918 }
919 catch( Exception e )
920 {
921 WorkflowDeploySiteContext workflowDeploySiteContext = _workflowDeploySiteService
922 .getWorkflowDeploySiteContext( _nIdCurrentWorkflowDeploySiteContext );
923
924 AppLogService.error( "Error processing during deployment of application '" + workflowDeploySiteContext.getIdApplication( ) + "' - cause : "
925 + e.getMessage( ), e );
926 }
927 }
928
929 return null;
930 }
931
932 public String getJSONCommandResult( HttpServletRequest request )
933 {
934 WorkflowDeploySiteContext workflowDeploySiteContext = _workflowDeploySiteService.getWorkflowDeploySiteContext( _nIdCurrentWorkflowDeploySiteContext );
935 CommandResult result = workflowDeploySiteContext.getCommandResult( );
936 String strReturn = ConstanteUtils.CONSTANTE_EMPTY_STRING;
937
938 if ( result != null )
939 {
940 JSONObject jo = DeploymentUtils.getJSONForCommandResult( result );
941 strReturn = jo.toString( );
942 }
943
944 return strReturn;
945 }
946
947 public String doDeployApplication( HttpServletRequest request ) throws AccessDeniedException
948 {
949 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
950 int nIdApplication = DeploymentUtils.getIntegerParameter( strIdApplication );
951
952 if ( ( request.getParameter( ConstanteUtils.PARAM_CANCEL ) == null ) && ( nIdApplication != ConstanteUtils.CONSTANTE_ID_NULL ) )
953 {
954 WorkflowDeploySiteContext workflowContext = new WorkflowDeploySiteContext( );
955 String strError = getFormDeployData( request, workflowContext );
956
957 if ( strError != null )
958 {
959 return strError;
960 }
961
962 Application application = _applicationService.getApplication( nIdApplication, getPlugin( ) );
963
964 if ( !StringUtils.isEmpty( workflowContext.getScriptFileSelected( ) ) )
965 {
966
967 try
968 {
969 workflowContext.setScriptFileItem( new FileInputStream( DeploymentUtils.getPathUpgradeFile(
970 DeploymentUtils.getPathCheckoutSite( application.getArtifactId( ) ), workflowContext.getScriptFileSelected( ) ) ) );
971 }
972 catch( FileNotFoundException e )
973 {
974
975 AppLogService.error( e );
976 }
977 workflowContext.setScriptFileItemName( workflowContext.getScriptFileSelected( ) );
978 }
979
980 HttpSession session = request.getSession( true );
981 if ( session.getAttribute( ConstanteUtils.ATTRIBUTE_VCS_USER ) != null )
982 {
983 AbstractVCSUser user = new AbstractVCSUser( );
984 user = (AbstractVCSUser) session.getAttribute( ConstanteUtils.ATTRIBUTE_VCS_USER );
985 workflowContext.setVcsUser( user );
986 }
987 else
988 {
989 workflowContext.setVcsUser( null );
990 }
991
992 workflowContext.setIdApplication( application.getIdApplication( ) );
993 workflowContext.setSvnBaseSiteUrl( application.getUrlRepo( ) );
994
995 CommandResult commandResult = new CommandResult( );
996
997
998
999 _nIdCurrentWorkflowDeploySiteContext = _workflowDeploySiteService.addWorkflowDeploySiteContext( workflowContext );
1000
1001 return getJspDeployApplicationProcess( request );
1002 }
1003
1004 return getJspManageApplication( request );
1005 }
1006
1007
1008
1009
1010
1011
1012
1013
1014 public String getTasksForm( HttpServletRequest request )
1015 {
1016 String strIdAction = request.getParameter( ConstanteUtils.PARAM_ID_ACTION );
1017
1018 if ( ( _nIdCurrentWorkflowDeploySiteContext != null ) && StringUtils.isNotBlank( strIdAction ) && StringUtils.isNumeric( strIdAction ) )
1019 {
1020 int nIdAction = DeploymentUtils.getIntegerParameter( strIdAction );
1021
1022 String strHtmlTasksForm = WorkflowService.getInstance( ).getDisplayTasksForm( _nIdCurrentWorkflowDeploySiteContext,
1023 WorkflowDeploySiteContext.WORKFLOW_RESOURCE_TYPE, nIdAction, request, getLocale( ) );
1024
1025 Map<String, Object> model = new HashMap<String, Object>( );
1026
1027 model.put( ConstanteUtils.MARK_TASKS_FORM, strHtmlTasksForm );
1028 model.put( ConstanteUtils.MARK_ID_ACTION, nIdAction );
1029
1030 setPageTitleProperty( ConstanteUtils.PROPERTY_TASKS_FORM_WORKFLOW_PAGE_TITLE );
1031
1032 HtmlTemplate templateList = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_TASKS_FORM_WORKFLOW, getLocale( ), model );
1033
1034 return templateList.getHtml( );
1035 }
1036
1037 return getManageApplication( request );
1038 }
1039
1040
1041
1042
1043
1044
1045
1046
1047 public String getFormActionServer( HttpServletRequest request )
1048 {
1049
1050 Plugin plugin = PluginService.getPlugin( DeploymentPlugin.PLUGIN_NAME );
1051 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
1052 String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
1053 String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
1054 String strServerAppicationType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
1055 String strActionCode = request.getParameter( ConstanteUtils.PARAM_ACTION_CODE );
1056
1057 Application application = _applicationService.getApplication( DeploymentUtils.getIntegerParameter( strIdApplication ), plugin );
1058 IAction action = _actionService.getAction( DeploymentUtils.getActionKey( strActionCode, strServerAppicationType ), request.getLocale( ) );
1059
1060 ServerApplicationInstance serverApplicationInstance = _serverApplicationService.getServerApplicationInstance( application,
1061 strCodeServerApplicationInstance, strCodeEnvironment, strServerAppicationType, request.getLocale( ), false, false );
1062
1063 if ( action != null )
1064 {
1065 String strHtmlFormAction = action.getTemplateFormAction( application, serverApplicationInstance, request.getLocale( ) );
1066
1067 Map<String, Object> model = new HashMap<String, Object>( );
1068
1069 model.put( ConstanteUtils.MARK_FORM_ACTION, strHtmlFormAction );
1070
1071 model.put( ConstanteUtils.PARAM_ID_APPLICATION, application.getIdApplication( ) );
1072 model.put( ConstanteUtils.PARAM_ACTION_CODE, strActionCode );
1073 model.put( ConstanteUtils.PARAM_CODE_ENVIRONMENT, strCodeEnvironment );
1074 model.put( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE, strCodeServerApplicationInstance );
1075 model.put( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE, strServerAppicationType );
1076
1077 setPageTitleProperty( ConstanteUtils.PROPERTY_FORM_ACTION_SERVER_PAGE_TITLE );
1078
1079 HtmlTemplate templateList = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_FORM_ACTION_SERVER, getLocale( ), model );
1080
1081 return templateList.getHtml( );
1082 }
1083
1084 return getManageApplication( request );
1085 }
1086
1087 public String doRunActionServer( HttpServletRequest request ) throws AccessDeniedException
1088 {
1089 CommandResult commandResult = new CommandResult( );
1090 Plugin plugin = PluginService.getPlugin( DeploymentPlugin.PLUGIN_NAME );
1091 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
1092 String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
1093 String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
1094 String strServerAppicationType = request.getParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE );
1095 String strActionCode = request.getParameter( ConstanteUtils.PARAM_ACTION_CODE );
1096 String strJspFormToDisplay = null;
1097
1098 Environment environment = _environmentService.getEnvironment( strCodeEnvironment, getLocale( ) );
1099
1100 Application application = _applicationService.getApplication( DeploymentUtils.getIntegerParameter( strIdApplication ), plugin );
1101 IAction action = _actionService.getAction( DeploymentUtils.getActionKey( strActionCode, strServerAppicationType ), request.getLocale( ) );
1102
1103 ServerApplicationInstance serverApplicationInstance = _serverApplicationService.getServerApplicationInstance( application,
1104 strCodeServerApplicationInstance, strCodeEnvironment, strServerAppicationType, request.getLocale( ), false, false );
1105
1106 List<IAction> listNewServersActions = null;
1107 Integer nNewServersStatus = null;
1108
1109 if ( action != null )
1110 {
1111
1112 if ( !isAuthorized( application, ApplicationResourceIdService.PERMISSION_VIEW, environment ) )
1113 {
1114
1115 commandResult.setError( I18nService.getLocalizedString( MESSAGE_ACCESS_DENIED, getLocale( ) ) );
1116 commandResult.setStatus( CommandResult.STATUS_ERROR );
1117
1118 return DeploymentUtils.getJSONForCommandResult( commandResult ).toString( );
1119
1120
1121 }
1122
1123 DeploymentUtils.startCommandResult( commandResult );
1124
1125 if ( _actionService.canExecuteAction( application, action, serverApplicationInstance, commandResult,
1126 DeploymentUtils.getActionParameters( request, action.getParameters( ) ) ) )
1127 {
1128
1129 commandResult.getLog( ).append( "Starting Action " + action.getName( ) + " \n" );
1130
1131 _actionService.executeAction( application, action, serverApplicationInstance, commandResult,
1132 DeploymentUtils.getActionParameters( request, action.getParameters( ) ) );
1133 commandResult.getLog( ).append( "End Action " + action.getName( ) + " \n" );
1134
1135 DeploymentUtils.stopCommandResult( commandResult );
1136
1137
1138 serverApplicationInstance = _serverApplicationService.getServerApplicationInstance( application, strCodeServerApplicationInstance,
1139 strCodeEnvironment, strServerAppicationType, request.getLocale( ), true, true );
1140 listNewServersActions = serverApplicationInstance.getListServerApplicationAction( );
1141 nNewServersStatus = serverApplicationInstance.getStatus( );
1142 }
1143 else
1144 if ( _actionService.getTemplateFormAction( application, action, serverApplicationInstance, getLocale( ) ) != null )
1145 {
1146 strJspFormToDisplay = getJspFormActionServer( request, strActionCode, DeploymentUtils.getIntegerParameter( strIdApplication ),
1147 strCodeEnvironment, strCodeServerApplicationInstance, strServerAppicationType );
1148 }
1149
1150 }
1151
1152 JSONObject jo = DeploymentUtils.getJSONForServerAction( application.getIdApplication( ), serverApplicationInstance.getCodeEnvironment( ),
1153 serverApplicationInstance.getCode( ), serverApplicationInstance.getType( ), strJspFormToDisplay, commandResult, listNewServersActions,
1154 nNewServersStatus );
1155
1156 return jo.toString( );
1157
1158 }
1159
1160 public String doModifyMavenProfil( HttpServletRequest request ) throws AccessDeniedException
1161 {
1162 Plugin plugin = PluginService.getPlugin( DeploymentPlugin.PLUGIN_NAME );
1163 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
1164 String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
1165 String strCodeServerApplicationInstance = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE );
1166 String strMavenProfil = request.getParameter( ConstanteUtils.PARAM_MAVEN_PROFIL );
1167
1168 Environment environment = _environmentService.getEnvironment( strCodeEnvironment, getLocale( ) );
1169
1170 Application application = _applicationService.getApplication( DeploymentUtils.getIntegerParameter( strIdApplication ), plugin );
1171
1172 if ( isAuthorized( application, ApplicationResourceIdService.PERMISSION_VIEW, environment ) )
1173 {
1174
1175 MavenService.getService( ).saveMvnProfilName( strMavenProfil, strIdApplication, strCodeEnvironment, strCodeServerApplicationInstance );
1176 }
1177
1178 return getJspViewApplication( request, DeploymentUtils.getIntegerParameter( strIdApplication ) );
1179
1180 }
1181
1182 private String getFormDeployData( HttpServletRequest request, WorkflowDeploySiteContext workflowDeploySiteContext )
1183 {
1184 String strCodeEnvironment = request.getParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT );
1185
1186 String strDeployWar = request.getParameter( ConstanteUtils.PARAM_DEPLOY_WAR );
1187 String strDeploySql = request.getParameter( ConstanteUtils.PARAM_DEPLOY_SQL );
1188
1189 String strCodeServerApplicationInstanceTomcat = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE_TOMCAT );
1190 String strCodeServerApplicationInstanceMysql = request.getParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE_SQL );
1191 String strDeployDevSite = request.getParameter( ConstanteUtils.PARAM_DEPLOY_DEV_SITE );
1192 String strTagToDeploy = request.getParameter( ConstanteUtils.PARAM_TAG_TO_DEPLOY );
1193
1194 String strCodeDatabase = request.getParameter( ConstanteUtils.PARAM_CODE_DATABASE );
1195 String strScriptUpgradeSelected = request.getParameter( ConstanteUtils.PARAM_SCRIPT_UPGRADE_SELECTED );
1196 String strInitDatabase = request.getParameter( ConstanteUtils.PARAM_INIT_DATABASE );
1197 String strInitAppContext = request.getParameter( ConstanteUtils.PARAM_INIT_APP_CONTEXT );
1198
1199 MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
1200 FileItem scriptItem = null;
1201
1202 String strFieldError = ConstanteUtils.CONSTANTE_EMPTY_STRING;
1203
1204 if ( StringUtils.isEmpty( strCodeEnvironment ) )
1205 {
1206 strFieldError = ConstanteUtils.PROPERTY_LABEL_CODE_ENVIRONMENT;
1207 }
1208 else
1209 if ( !StringUtils.isEmpty( strDeployWar ) && StringUtils.isEmpty( strCodeServerApplicationInstanceTomcat ) )
1210 {
1211 strFieldError = ConstanteUtils.PROPERTY_LABEL_CODE_SERVER_APPLICATION_INSTANCE_TOMCAT;
1212 }
1213 else
1214 if ( !StringUtils.isEmpty( strDeploySql ) && StringUtils.isEmpty( strCodeServerApplicationInstanceMysql ) )
1215 {
1216 strFieldError = ConstanteUtils.PROPERTY_LABEL_CODE_SERVER_APPLICATION_INSTANCE_MYSQL;
1217 }
1218 else
1219 if ( !StringUtils.isEmpty( strDeploySql ) && StringUtils.isEmpty( strCodeDatabase ) && StringUtils.isEmpty( strInitDatabase ) )
1220 {
1221 strFieldError = ConstanteUtils.PROPERTY_LABEL_CODE_DATABASE;
1222 }
1223 else
1224 if ( !StringUtils.isEmpty( strDeployWar ) && StringUtils.isEmpty( strDeployDevSite ) && StringUtils.isEmpty( strTagToDeploy ) )
1225 {
1226 strFieldError = ConstanteUtils.PROPERTY_LABEL_TAG_TO_DEPLOY;
1227 }
1228 else
1229 if ( !StringUtils.isEmpty( strDeploySql ) )
1230 {
1231 List<FileItem> scriptListItem = _handler.getListUploadedFiles( ConstanteUtils.PARAM_SCRIPT_UPLOAD, request.getSession( ) );
1232 if ( !CollectionUtils.isEmpty( scriptListItem ) )
1233 {
1234 scriptItem = scriptListItem.get( 0 );
1235
1236 }
1237 if ( StringUtils.isEmpty( strScriptUpgradeSelected ) && StringUtils.isEmpty( strInitDatabase )
1238 && ( scriptItem == null || scriptItem.getSize( ) == 0 ) )
1239 {
1240 strFieldError = ConstanteUtils.PROPERTY_LABEL_SCRIPT_UPLOAD;
1241 }
1242 }
1243
1244 if ( !StringUtils.isEmpty( strFieldError ) )
1245 {
1246 Object [ ] tabRequiredFields = {
1247 I18nService.getLocalizedString( strFieldError, getLocale( ) )
1248 };
1249
1250 return AdminMessageService.getMessageUrl( request, ConstanteUtils.PROPERTY_MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
1251 }
1252
1253 workflowDeploySiteContext.setCodeEnvironement( strCodeEnvironment );
1254
1255 if ( !StringUtils.isEmpty( strCodeServerApplicationInstanceTomcat ) )
1256 {
1257 workflowDeploySiteContext.setCodeServerInstance( strCodeServerApplicationInstanceTomcat, ConstanteUtils.CONSTANTE_SERVER_TOMCAT );
1258 }
1259
1260 if ( !StringUtils.isEmpty( strCodeServerApplicationInstanceMysql ) )
1261 {
1262 workflowDeploySiteContext.setCodeServerInstance( strCodeServerApplicationInstanceMysql.split( "_" ) [0],
1263 strCodeServerApplicationInstanceMysql.split( "_" ) [1] );
1264
1265 }
1266
1267 workflowDeploySiteContext.setDeployDevSite( ( strDeployDevSite != null ) ? true : false );
1268 workflowDeploySiteContext.setDeployWar( !StringUtils.isEmpty( strDeployWar ) );
1269 workflowDeploySiteContext.setDeploySql( !StringUtils.isEmpty( strDeploySql ) );
1270 workflowDeploySiteContext.setInitAppContext( !StringUtils.isEmpty( strInitAppContext ) );
1271 workflowDeploySiteContext.setInitBdd( !StringUtils.isEmpty( strInitDatabase ) );
1272
1273 if ( StringUtils.isEmpty( strDeployDevSite ) )
1274 {
1275 workflowDeploySiteContext.setTagToDeploy( strTagToDeploy );
1276 workflowDeploySiteContext.setDeployDevSite( false );
1277 }
1278 else
1279 {
1280 workflowDeploySiteContext.setDeployDevSite( true );
1281 }
1282
1283 if ( !StringUtils.isEmpty( strDeploySql ) )
1284 {
1285 workflowDeploySiteContext.setDatabaseName( strCodeDatabase );
1286 workflowDeploySiteContext.setInitBdd( !StringUtils.isEmpty( strInitDatabase ) );
1287
1288 if ( !StringUtils.isEmpty( strScriptUpgradeSelected ) )
1289 {
1290 workflowDeploySiteContext.setScriptFileSelected( strScriptUpgradeSelected );
1291 }
1292 else
1293 {
1294 try
1295 {
1296 workflowDeploySiteContext.setScriptFileItem( scriptItem.getInputStream( ) );
1297 }
1298 catch( IOException e )
1299 {
1300 AppLogService.error( e );
1301 }
1302 workflowDeploySiteContext.setScriptFileItemName( scriptItem.getFieldName( ) );
1303
1304 }
1305 }
1306
1307 return null;
1308 }
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321 private String getApplicationData( HttpServletRequest request, Application application )
1322 {
1323 String strCode = request.getParameter( ConstanteUtils.PARAM_CODE );
1324 String strName = request.getParameter( ConstanteUtils.PARAM_NAME );
1325 String strWebAppName = request.getParameter( ConstanteUtils.PARAM_WEBAPP_NAME );
1326 String strWorkgroup = request.getParameter( ConstanteUtils.PARAM_WORKGROUP );
1327 String strUrlRepo = request.getParameter( ConstanteUtils.PARAM_URL_REPO );
1328
1329 String strFieldError = ConstanteUtils.CONSTANTE_EMPTY_STRING;
1330
1331 if ( StringUtils.isEmpty( strCode ) )
1332 {
1333 strFieldError = ConstanteUtils.PROPERTY_LABEL_CODE;
1334 }
1335 else
1336 if ( StringUtils.isEmpty( strName ) )
1337 {
1338 strFieldError = ConstanteUtils.PROPERTY_LABEL_NAME;
1339 }
1340 else
1341 if ( StringUtils.isEmpty( strWebAppName ) )
1342 {
1343 strFieldError = ConstanteUtils.PROPERTY_LABEL_WEBAPP_NAME;
1344 }
1345 else
1346 if ( StringUtils.isEmpty( strUrlRepo ) )
1347 {
1348 strFieldError = ConstanteUtils.PROPERTY_LABEL_URL_REPO;
1349 }
1350
1351 if ( !StringUtils.isEmpty( strFieldError ) )
1352 {
1353 Object [ ] tabRequiredFields = {
1354 I18nService.getLocalizedString( strFieldError, getLocale( ) )
1355 };
1356
1357 return AdminMessageService.getMessageUrl( request, ConstanteUtils.PROPERTY_MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
1358 }
1359
1360 application.setCode( strCode );
1361 application.setName( strName );
1362 application.setWebAppName( strWebAppName );
1363 application.setWorkgroup( strWorkgroup );
1364 application.setUrlRepo( strUrlRepo );
1365
1366 try
1367 {
1368 RepositoryUtils.checkRepository( application );
1369 }
1370 catch( InvalidRepositoryUrlException e )
1371 {
1372 return AdminMessageService.getMessageUrl( request, ConstanteUtils.PROPERTY_MESSAGE_INVALID_REPO_URL, AdminMessage.TYPE_STOP );
1373 }
1374
1375 return null;
1376 }
1377
1378 private String getJspManageApplication( HttpServletRequest request )
1379 {
1380 return AppPathService.getBaseUrl( request ) + ConstanteUtils.JSP_MANAGE_APPLICATION;
1381 }
1382
1383 private String getJspViewApplication( HttpServletRequest request, int nIdApplication )
1384 {
1385 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + ConstanteUtils.JSP_VIEW_APPLICATION );
1386 url.addParameter( ConstanteUtils.PARAM_ID_APPLICATION, nIdApplication );
1387
1388 return url.getUrl( );
1389 }
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404 private String getJspTasksForm( HttpServletRequest request, int nIdWorkFlowContext, int nIdAction )
1405 {
1406 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + ConstanteUtils.JSP_TASK_FORM );
1407 url.addParameter( ConstanteUtils.PARAM_ID_ACTION, nIdAction );
1408 url.addParameter( ConstanteUtils.PARAM_ID_WORKFLOW_CONTEXT, nIdWorkFlowContext );
1409
1410 return url.getUrl( );
1411 }
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426 private String getJspFormActionServer( HttpServletRequest request, String strActionCode, int nIdApplication, String strCodeEnvironment,
1427 String strCodeServerApplicationInstance, String strServerAppicationType )
1428 {
1429
1430 UrlItem url = new UrlItem( AppPathService.getBaseUrl( request ) + ConstanteUtils.JSP_FORM_SERVER_ACTION );
1431 url.addParameter( ConstanteUtils.PARAM_ID_APPLICATION, nIdApplication );
1432 url.addParameter( ConstanteUtils.PARAM_ACTION_CODE, strActionCode );
1433 url.addParameter( ConstanteUtils.PARAM_CODE_ENVIRONMENT, strCodeEnvironment );
1434 url.addParameter( ConstanteUtils.PARAM_CODE_SERVER_APPLICATION_INSTANCE, strCodeServerApplicationInstance );
1435 url.addParameter( ConstanteUtils.PARAM_SERVER_APPLICATION_TYPE, strServerAppicationType );
1436
1437 return url.getUrl( );
1438 }
1439
1440 private String getJspDeployApplicationProcess( HttpServletRequest request )
1441 {
1442 return AppPathService.getBaseUrl( request ) + ConstanteUtils.JSP_DEPLOY_APPLICATION_PROCESS;
1443 }
1444
1445 private boolean isAuthorized( Application application, String strPermission )
1446 {
1447
1448 return RBACService.isAuthorized( Application.RESOURCE_TYPE, Integer.toString( application.getIdApplication( ) ), strPermission, getUser( ) )
1449 && AdminWorkgroupService.isAuthorized( application, getUser( ) );
1450
1451 }
1452
1453 private boolean isAuthorized( Application application, String strPermission, Environment environment )
1454 {
1455
1456 return RBACService.isAuthorized( Application.RESOURCE_TYPE, Integer.toString( application.getIdApplication( ) ), strPermission, getUser( ) )
1457 && AdminWorkgroupService.isAuthorized( application, getUser( ) )
1458 && RBACService.isAuthorized( Environment.RESOURCE_TYPE, environment.getResourceId( ),
1459 EnvironmentResourceIdService.PERMISSION_DEPLOY_ON_ENVIROMENT, getUser( ) );
1460
1461 }
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471 public String getFormAskCredentials( HttpServletRequest request ) throws AccessDeniedException
1472 {
1473 String strIdApplication = request.getParameter( ConstanteUtils.PARAM_ID_APPLICATION );
1474 int nIdApplication = Integer.parseInt( strIdApplication );
1475 String strActionUrl = request.getParameter( ConstanteUtils.PARAM_ACTION_URL );
1476 Application application = _applicationService.getApplication( nIdApplication, getPlugin( ) );
1477 if ( ApplicationService.isPrivateRepo( application ) )
1478 {
1479 HttpSession session = request.getSession( true );
1480 AbstractVCSUser user = (AbstractVCSUser)session.getAttribute( ConstanteUtils.ATTRIBUTE_VCS_USER );
1481
1482 Map<String, Object> model = new HashMap<>( );
1483 model.put( ConstanteUtils.MARK_VCS_SERVICE, DeploymentUtils.getVCSService( application.getRepoType( ) ) );
1484 model.put( ConstanteUtils.MARK_APPLICATION, application );
1485 model.put( ConstanteUtils.MARK_ACTION_URL, strActionUrl );
1486 if ( user != null )
1487 {
1488 model.put( ConstanteUtils.MARK_USER, user );
1489 }
1490
1491
1492 HtmlTemplate template = AppTemplateService.getTemplate( ConstanteUtils.TEMPLATE_ASK_CREDENTIALS, getLocale( ), model );
1493 return template.getHtml( );
1494 }
1495 else
1496 {
1497
1498 return doDeployApplication( request );
1499 }
1500 }
1501
1502
1503
1504
1505
1506
1507
1508
1509 public String checkRepository( HttpServletRequest request )
1510 {
1511 String strRepoUrl = request.getParameter( ConstanteUtils.PARAM_URL_REPO );
1512 return RepositoryUtils.checkRepository( strRepoUrl );
1513 }
1514
1515 }