GestorePolicyAttiveWS.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.pdd.core.controllo_traffico.policy.driver;

  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.openspcoop2.core.controllo_traffico.beans.ActivePolicy;
  27. import org.openspcoop2.core.controllo_traffico.beans.ConfigurazioneControlloTraffico;
  28. import org.openspcoop2.core.controllo_traffico.beans.DatiTransazione;
  29. import org.openspcoop2.core.controllo_traffico.beans.UniqueIdentifierUtilities;
  30. import org.openspcoop2.core.controllo_traffico.driver.CostantiServizioControlloTraffico;
  31. import org.openspcoop2.core.controllo_traffico.driver.IGestorePolicyAttive;
  32. import org.openspcoop2.core.controllo_traffico.driver.IPolicyGroupByActiveThreads;
  33. import org.openspcoop2.core.controllo_traffico.driver.PolicyException;
  34. import org.openspcoop2.core.controllo_traffico.driver.PolicyGroupByActiveThreadsType;
  35. import org.openspcoop2.core.controllo_traffico.driver.PolicyNotFoundException;
  36. import org.openspcoop2.core.controllo_traffico.driver.PolicyShutdownException;
  37. import org.openspcoop2.utils.transport.TransportUtils;
  38. import org.openspcoop2.utils.transport.http.HttpResponse;
  39. import org.openspcoop2.utils.transport.http.HttpUtilities;
  40. import org.slf4j.Logger;

  41. /**    
  42.  * GestorePolicyAttiveWS
  43.  *
  44.  * @author Poli Andrea (poli@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */
  48. public class GestorePolicyAttiveWS implements IGestorePolicyAttive {

  49.     private static final String IMPL_DESCR = "Implementazione WS IGestorePolicyAttive";
  50.     public static String getImplDescr(){
  51.         return IMPL_DESCR;
  52.     }
  53.    
  54.     private Logger log;
  55.     private PolicyGroupByActiveThreadsType type;
  56.     private String uriService;
  57.     @Override
  58.     public void initialize(Logger log, boolean isStartupGovWay, PolicyGroupByActiveThreadsType type, Object ... params) throws PolicyException{
  59.         this.log = log;
  60.         this.type = type;
  61.         if(this.type==null) {
  62.             this.type = PolicyGroupByActiveThreadsType.LOCAL;
  63.         }
  64.         if(params.length<=0){
  65.             throw new PolicyException("URI Service not found");
  66.         }
  67.         if(params[0] == null || !(params[0] instanceof String)){
  68.             throw new PolicyException("URI Service not found ("+params[0]+")");
  69.         }
  70.         this.uriService = ((String) params[0]).trim();
  71.         if(this.uriService.endsWith("/")==false){
  72.             this.uriService = this.uriService + "/";
  73.         }
  74.     }
  75.    
  76.     @Override
  77.     public PolicyGroupByActiveThreadsType getType() {
  78.         return this.type;
  79.     }
  80.    
  81.     @Override
  82.     public IPolicyGroupByActiveThreads getActiveThreadsPolicy(ActivePolicy activePolicy, DatiTransazione datiTransazione, Object state)
  83.             throws PolicyShutdownException,PolicyException {
  84.        
  85.         try{
  86.             String activeId = UniqueIdentifierUtilities.getUniqueId(activePolicy.getInstanceConfiguration());
  87.             Map<String, List<String>> p = new HashMap<>();
  88.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_ACTIVE_ID, activeId);
  89.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_REGISTER_POLICY, this.log);
  90.            
  91.             this.log.debug("[GestorePolicyAttiveWS.getActiveThreadsPolicy(ActivePolicy)] invoke ("+url+") ...");
  92.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  93.             this.log.debug("[GestorePolicyAttiveWS.getActiveThreadsPolicy(ActivePolicy)] invoked with code ["+response.getResultHTTPOperation()+"]");
  94.            
  95.             if(response.getResultHTTPOperation()!=200){
  96.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  97.             }
  98.            
  99.             PolicyGroupByActiveThreadsWS group = new PolicyGroupByActiveThreadsWS(this.uriService, activeId, this.log);
  100.             return group;
  101.            
  102.         }catch(Exception e){
  103.             this.log.error("getActiveThreadsPolicy(ActivePolicy) error: "+e.getMessage(),e);
  104.             throw new PolicyException(e.getMessage(),e);
  105.         }
  106.        
  107.     }
  108.    
  109.     @Override
  110.     public IPolicyGroupByActiveThreads getActiveThreadsPolicy(String activeId)
  111.             throws PolicyShutdownException, PolicyException, PolicyNotFoundException {
  112.         try{
  113.             Map<String, List<String>> p = new HashMap<>();
  114.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_ACTIVE_ID, activeId);
  115.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_GET_POLICY, this.log);
  116.            
  117.             this.log.debug("[GestorePolicyAttiveWS.getActiveThreadsPolicy] invoke ("+url+") ...");
  118.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  119.             this.log.debug("[GestorePolicyAttiveWS.getActiveThreadsPolicy] invoked with code ["+response.getResultHTTPOperation()+"]");
  120.            
  121.             if(response.getResultHTTPOperation()!=200){
  122.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  123.             }
  124.            
  125.             PolicyGroupByActiveThreadsWS group = new PolicyGroupByActiveThreadsWS(this.uriService, activeId, this.log);
  126.             return group;
  127.            
  128.         }catch(Exception e){
  129.             this.log.error("getActiveThreadsPolicy(id) error: "+e.getMessage(),e);
  130.             throw new PolicyException(e.getMessage(),e);
  131.         }
  132.     }
  133.    

  134.     @Override
  135.     public long sizeActivePolicyThreads(boolean sum) throws PolicyShutdownException,PolicyException {
  136.         try{
  137.             Map<String, List<String>> p = new HashMap<>();
  138.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_SUM, sum+"");
  139.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_SIZE_ACTIVE_THREADS_POLICY, this.log);
  140.            
  141.             this.log.debug("[GestorePolicyAttiveWS.sizeActivePolicyThreads] invoke ("+url+") ...");
  142.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  143.             this.log.debug("[GestorePolicyAttiveWS.sizeActivePolicyThreads] invoked with code ["+response.getResultHTTPOperation()+"]");
  144.            
  145.             if(response.getResultHTTPOperation()!=200){
  146.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  147.             }
  148.            
  149.             return Long.parseLong(new String(response.getContent()));
  150.            
  151.         }catch(Exception e){
  152.             this.log.error("sizeActivePolicyThreads error: "+e.getMessage(),e);
  153.             throw new PolicyException(e.getMessage(),e);
  154.         }
  155.     }
  156.    
  157.     @Override
  158.     public String printKeysPolicy(String separator) throws PolicyShutdownException, PolicyException {
  159.         try{
  160.             Map<String, List<String>> p = new HashMap<>();
  161.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_SEPARATOR, separator);
  162.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_PRINT_KEYS_POLICY, this.log);
  163.            
  164.             this.log.debug("[GestorePolicyAttiveWS.printKeysPolicy] invoke ("+url+") ...");
  165.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  166.             this.log.debug("[GestorePolicyAttiveWS.printKeysPolicy] invoked with code ["+response.getResultHTTPOperation()+"]");
  167.            
  168.             if(response.getResultHTTPOperation()!=200){
  169.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  170.             }
  171.            
  172.             return new String(response.getContent());
  173.            
  174.         }catch(Exception e){
  175.             this.log.error("printKeysPolicy error: "+e.getMessage(),e);
  176.             throw new PolicyException(e.getMessage(),e);
  177.         }
  178.     }
  179.    
  180.     @Override
  181.     public String printInfoPolicy(String id, String separatorGroups)
  182.             throws PolicyShutdownException, PolicyException, PolicyNotFoundException {
  183.         try{
  184.             Map<String, List<String>> p = new HashMap<>();
  185.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_ACTIVE_ID, id);
  186.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_SEPARATOR, separatorGroups);
  187.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_PRINT_INFO_POLICY, this.log);
  188.            
  189.             this.log.debug("[GestorePolicyAttiveWS.printInfoPolicy] invoke ("+url+") ...");
  190.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  191.             this.log.debug("[GestorePolicyAttiveWS.printInfoPolicy] invoked with code ["+response.getResultHTTPOperation()+"]");
  192.            
  193.             if(response.getResultHTTPOperation()!=200){
  194.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  195.             }
  196.            
  197.             return new String(response.getContent());
  198.            
  199.         }catch(Exception e){
  200.             this.log.error("printInfoPolicy error: "+e.getMessage(),e);
  201.             throw new PolicyException(e.getMessage(),e);
  202.         }
  203.     }
  204.     @Override
  205.     public void removeActiveThreadsPolicy(String idActivePolicy) throws PolicyShutdownException, PolicyException {
  206.         try{
  207.             Map<String, List<String>> p = new HashMap<>();
  208.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_ACTIVE_ID, idActivePolicy);
  209.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_REMOVE_ACTIVE_THREADS_POLICY, this.log);
  210.            
  211.             this.log.debug("[GestorePolicyAttiveWS.removeActiveThreadsPolicy] invoke ("+url+") ...");
  212.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  213.             this.log.debug("[GestorePolicyAttiveWS.removeActiveThreadsPolicy] invoked with code ["+response.getResultHTTPOperation()+"]");
  214.            
  215.             if(response.getResultHTTPOperation()!=200){
  216.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  217.             }
  218.            
  219.         }catch(Exception e){
  220.             this.log.error("removeActiveThreadsPolicy error: "+e.getMessage(),e);
  221.             throw new PolicyException(e.getMessage(),e);
  222.         }
  223.     }
  224.    
  225.     @Override
  226.     public void removeActiveThreadsPolicyUnsafe(String idActivePolicy) throws PolicyShutdownException,PolicyException{
  227.         try{
  228.             Map<String, List<String>> p = new HashMap<>();
  229.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_ACTIVE_ID, idActivePolicy);
  230.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_REMOVE_ACTIVE_THREADS_POLICY_UNSAFE, this.log);
  231.            
  232.             this.log.debug("[GestorePolicyAttiveWS.removeActiveThreadsPolicyUnsafe] invoke ("+url+") ...");
  233.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  234.             this.log.debug("[GestorePolicyAttiveWS.removeActiveThreadsPolicyUnsafe] invoked with code ["+response.getResultHTTPOperation()+"]");
  235.            
  236.             if(response.getResultHTTPOperation()!=200){
  237.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  238.             }
  239.            
  240.         }catch(Exception e){
  241.             this.log.error("removeActiveThreadsPolicyUnsafe error: "+e.getMessage(),e);
  242.             throw new PolicyException(e.getMessage(),e);
  243.         }
  244.     }
  245.    
  246.     @Override
  247.     public void removeAllActiveThreadsPolicy() throws PolicyShutdownException, PolicyException {
  248.         try{
  249.             Map<String, List<String>> p = new HashMap<>();
  250.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_REMOVE_ALL_ACTIVE_THREADS_POLICY, this.log);
  251.            
  252.             this.log.debug("[GestorePolicyAttiveWS.removeAllActiveThreadsPolicy] invoke ("+url+") ...");
  253.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  254.             this.log.debug("[GestorePolicyAttiveWS.removeAllActiveThreadsPolicy] invoked with code ["+response.getResultHTTPOperation()+"]");
  255.            
  256.             if(response.getResultHTTPOperation()!=200){
  257.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  258.             }
  259.            
  260.         }catch(Exception e){
  261.             this.log.error("removeAllActiveThreadsPolicy error: "+e.getMessage(),e);
  262.             throw new PolicyException(e.getMessage(),e);
  263.         }
  264.     }
  265.     @Override
  266.     public void resetCountersActiveThreadsPolicy(String idActivePolicy)
  267.             throws PolicyShutdownException, PolicyException {
  268.         try{
  269.             Map<String, List<String>> p = new HashMap<>();
  270.             TransportUtils.setHeader(p,CostantiServizioControlloTraffico.PARAMETER_ACTIVE_ID, idActivePolicy);
  271.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_RESET_COUNTERS_ACTIVE_THREADS_POLICY, this.log);
  272.            
  273.             this.log.debug("[GestorePolicyAttiveWS.resetCountersActiveThreadsPolicy] invoke ("+url+") ...");
  274.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  275.             this.log.debug("[GestorePolicyAttiveWS.resetCountersActiveThreadsPolicy] invoked with code ["+response.getResultHTTPOperation()+"]");
  276.            
  277.             if(response.getResultHTTPOperation()!=200){
  278.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  279.             }
  280.            
  281.         }catch(Exception e){
  282.             this.log.error("resetCountersActiveThreadsPolicy error: "+e.getMessage(),e);
  283.             throw new PolicyException(e.getMessage(),e);
  284.         }
  285.     }
  286.     @Override
  287.     public void resetCountersAllActiveThreadsPolicy() throws PolicyShutdownException, PolicyException {
  288.         try{
  289.             Map<String, List<String>> p = new HashMap<>();
  290.             String url = TransportUtils.buildUrlWithParameters(p, this.uriService+CostantiServizioControlloTraffico.OPERAZIONE_RESET_COUNTERS_ALL_ACTIVE_THREADS_POLICY, this.log);
  291.            
  292.             this.log.debug("[GestorePolicyAttiveWS.resetCountersAllActiveThreadsPolicy] invoke ("+url+") ...");
  293.             HttpResponse response = HttpUtilities.getHTTPResponse(url);
  294.             this.log.debug("[GestorePolicyAttiveWS.resetCountersAllActiveThreadsPolicy] invoked with code ["+response.getResultHTTPOperation()+"]");
  295.            
  296.             if(response.getResultHTTPOperation()!=200){
  297.                 throw new Exception("[httpCode:"+response.getResultHTTPOperation()+"] "+new String(response.getContent()));
  298.             }
  299.            
  300.         }catch(Exception e){
  301.             this.log.error("resetCountersAllActiveThreadsPolicy error: "+e.getMessage(),e);
  302.             throw new PolicyException(e.getMessage(),e);
  303.         }
  304.     }
  305.     @Override
  306.     public void serialize(OutputStream out) throws PolicyException {
  307.         // nop;
  308.     }
  309.     @Override
  310.     public void initialize(InputStream in, ConfigurazioneControlloTraffico configurazioneControlloTraffico)
  311.             throws PolicyException {
  312.         // nop;
  313.     }
  314.     @Override
  315.     public void cleanOldActiveThreadsPolicy() throws PolicyException{
  316.         // nop;
  317.     }

  318.    
  319. }