View Javadoc
1   package fr.paris.lutece.plugins.appcenter.modules.openam.service;
2   
3   import java.io.IOException;
4   import java.util.List;
5   import java.util.Locale;
6   
7   import javax.servlet.http.HttpServletRequest;
8   
9   import org.apache.commons.lang3.StringUtils;
10  
11  import fr.paris.lutece.plugins.appcenter.business.Application;
12  import fr.paris.lutece.plugins.appcenter.business.ApplicationHome;
13  import fr.paris.lutece.plugins.appcenter.business.DemandHome;
14  import fr.paris.lutece.plugins.appcenter.business.Environment;
15  import fr.paris.lutece.plugins.appcenter.modules.openam.business.OpenamAgentData;
16  import fr.paris.lutece.plugins.appcenter.modules.openam.business.OpenamAgentsData;
17  import fr.paris.lutece.plugins.appcenter.modules.openam.business.OpenamDemand;
18  import fr.paris.lutece.plugins.appcenter.modules.openam.service.client.rest.OpenamAgentDTO;
19  import fr.paris.lutece.plugins.appcenter.modules.openam.service.client.rest.OpenamAgentRestService;
20  import fr.paris.lutece.plugins.appcenter.service.ApplicationService;
21  import fr.paris.lutece.plugins.appcenter.service.task.AppcenterTask;
22  import fr.paris.lutece.plugins.workflowcore.business.resource.ResourceHistory;
23  import fr.paris.lutece.util.httpaccess.HttpAccessException;
24  
25  public class OpenamTask extends AppcenterTask
26  {
27      @Override
28      public String getTitle( Locale locale )
29      {
30          // TODO
31          return "Openam Task";
32      }
33  
34      @Override
35      public void processTask( int nIdResourceHistory, HttpServletRequest request, Locale locale )
36      {
37             super.processTask(nIdResourceHistory, request, locale, OpenamAgentData.class, OpenamAgentsData.class,OpenamDemand.class);
38             
39             
40          
41             ResourceHistory resourceHistory = _resourceHistoryService.findByPrimaryKey( nIdResourceHistory );
42             OpenamDemand demand = DemandHome.findByPrimaryKey( resourceHistory.getIdResource( ) ,OpenamDemand.class);  
43             boolean bProd=demand.getEnvironment().equals( Environment.PROD);
44             Application application = ApplicationHome.findByPrimaryKey( demand.getIdApplication( ) );
45             OpenamAgentsData agentsData= ApplicationService.loadApplicationDataSubset(application,  OpenamAgentsData.class);  
46             List<OpenamAgentData> agentDtoList= agentsData.getListData();
47             
48             for(OpenamAgentData ag:agentDtoList) {
49          	   if(ag.getCreateAgent() && ag.getListIdDemandAssociated().stream().anyMatch( val -> val.equals(demand.getId()))) {
50          		   OpenamAgentDTO  agentDto= OpenamAgentDTO.buildAgentDto( ag, demand,bProd ); 
51  	        	    try {
52  	    	        	   
53  	   	        	    createOpenamAgent( agentDto ,bProd);
54  	   	           
55  	   	            }catch(HttpAccessException | IOException e) {
56  	   	        	  
57  	   	        	   throw new RuntimeException (e);
58  	   	            }
59  	        		   break;
60  	        	   }
61             }
62            
63             
64      }
65  
66      /**
67       * Create Openam Agent Profile 
68       * @param agentDto
69       * @throws HttpAccessException
70       * @throws IOException
71       */
72      public void createOpenamAgent( OpenamAgentDTO agentDto, boolean bProd ) throws HttpAccessException, IOException{
73      	
74      	String token=OpenamAgentRestService.getToken(agentDto.getServerUrl( ), agentDto);        
75          agentDto.setTockenId(token);
76          
77          if (token != null && StringUtils.isNotEmpty(token)) {
78          try {         	
79             		OpenamAgentRestService.createAgentProfile(agentDto.getServerUrl( ), token, agentDto,bProd);
80              	OpenamAgentRestService.updateAgentProfile(agentDto.getServerUrl( ), token, agentDto,bProd);
81                  	
82           }catch (HttpAccessException e) {
83                if(e.getResponseCode() == 409 && e.getMessage().equals("Resource already exists")) {
84                 		OpenamAgentRestService.updateAgentProfile(agentDto.getServerUrl( ), token, agentDto,bProd);
85               }else {
86                  		throw new HttpAccessException(e.getMessage(), e);
87               }
88           }finally{        		
89                  	OpenamAgentRestService.logout(agentDto.getServerUrl( ), token,bProd);
90                  }
91  	 
92           }
93           
94      }
95      
96     
97  }