PreInRequestHandler.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.handlers.transazioni;

  21. import java.util.List;

  22. import org.openspcoop2.core.constants.Costanti;
  23. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  24. import org.openspcoop2.pdd.core.PdDContext;
  25. import org.openspcoop2.pdd.core.controllo_traffico.handler.PreInRequestHandlerGestioneControlloTraffico;
  26. import org.openspcoop2.pdd.core.credenziali.Credenziali;
  27. import org.openspcoop2.pdd.core.handlers.HandlerException;
  28. import org.openspcoop2.pdd.core.handlers.PreInRequestContext;
  29. import org.openspcoop2.pdd.core.transazioni.Transaction;
  30. import org.openspcoop2.pdd.core.transazioni.TransactionContext;
  31. import org.openspcoop2.pdd.core.transazioni.TransactionDeletedException;
  32. import org.openspcoop2.pdd.core.transazioni.TransactionNotExistsException;
  33. import org.openspcoop2.pdd.services.connector.ConnectorException;
  34. import org.openspcoop2.pdd.services.connector.messages.ConnectorInMessage;
  35. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  36. import org.openspcoop2.utils.transport.TransportUtils;
  37. import org.openspcoop2.utils.transport.http.HttpUtilities;
  38. import org.slf4j.Logger;

  39. /**    
  40.  * PreInRequestHandler
  41.  *
  42.  * @author Poli Andrea (poli@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class PreInRequestHandler extends FirstPositionHandler implements org.openspcoop2.pdd.core.handlers.PreInRequestHandler{

  47.     // Viene realizzato come FirstPositionHandler per filtrare subito le violazioni di max threads
  48.    
  49.     @Override
  50.     public void invoke(PreInRequestContext context) throws HandlerException {
  51.        
  52.         OpenSPCoop2Properties op2Properties = OpenSPCoop2Properties.getInstance();
  53.         if(op2Properties.isTransazioniEnabled()==false) {
  54.             return;
  55.         }
  56.        
  57.         String idTransazione = (String) context.getPddContext().getObject(Costanti.ID_TRANSAZIONE);
  58.        
  59.         // Get Transazione
  60.         Transaction tr =  null;
  61.         try{
  62.             tr = TransactionContext.getTransaction(idTransazione);
  63.         }catch(TransactionNotExistsException e){
  64.             throw new HandlerException(e);
  65.             // Non dovrebbe avvenire in questo handler
  66.         }
  67.        
  68.         if(op2Properties.isControlloTrafficoEnabled()){
  69.             tr.getTempiElaborazione().startControlloTraffico_maxRequests();
  70.             try {
  71.                 PreInRequestHandlerGestioneControlloTraffico preInRequestHandler_gestioneControlloTraffico = new PreInRequestHandlerGestioneControlloTraffico();
  72.                 preInRequestHandler_gestioneControlloTraffico.process(context);
  73.             }finally {
  74.                 tr.getTempiElaborazione().endControlloTraffico_maxRequests();
  75.             }
  76.         }
  77.        
  78.         /* ---- Analisi Remote IP ----- */
  79.         ConnectorInMessage req = null;
  80.         try{
  81.             req = (ConnectorInMessage) context.getTransportContext().get(PreInRequestContext.SERVLET_REQUEST);
  82.         }catch(Throwable e){
  83.             context.getLogCore().error("Errore durante il recupero delle informazioni servlet: "+e.getMessage(),e);
  84.         }
  85.         if(req!=null){
  86.             readClientAddress(context.getLogCore(), req, context.getPddContext());
  87.         }
  88.                        
  89.         /* ---- Analisi RequestInfo ----- */
  90.         RequestInfo requestInfo = null;
  91.         try{
  92.             if(context.getRequestInfo()!=null) {
  93.                 requestInfo = context.getRequestInfo();
  94.             }
  95.             else {
  96.                 requestInfo = (RequestInfo) context.getPddContext().getObject(org.openspcoop2.core.constants.Costanti.REQUEST_INFO);
  97.             }
  98.            
  99.             setInfoInvocation(tr, requestInfo, req);
  100.            
  101.         }
  102.         catch(TransactionDeletedException e){
  103.             throw new HandlerException(e);
  104.             // Non dovrebbe avvenire in questo handler
  105.         }
  106.         catch(Throwable e){
  107.             context.getLogCore().error("Errore durante il recupero delle informazioni dall'oggetto request info: "+e.getMessage(),e);
  108.         }
  109.     }

  110.     public static void readClientAddress(Logger log,ConnectorInMessage req, PdDContext pddContext) {
  111.         if(req!=null){
  112.             try{
  113.                 String remoteAddr = req.getRemoteAddress();
  114.                 if(remoteAddr!=null){
  115.                     pddContext.addObject(Costanti.CLIENT_IP_REMOTE_ADDRESS,remoteAddr);
  116.                 }
  117.             }catch(Throwable e){
  118.                 log.error("Errore durante l'identificazione dell'indirizzo ip del chiamante (via socket): "+e.getMessage(),e);
  119.             }
  120.             try{
  121.                 String transportAddr = getIPClientAddressFromHeader(HttpUtilities.getClientAddressHeaders(), req);
  122.                 if(transportAddr!=null){
  123.                     pddContext.addObject(Costanti.CLIENT_IP_TRANSPORT_ADDRESS,transportAddr);
  124.                 }
  125.             }catch(Throwable e){
  126.                 log.error("Errore durante l'identificazione dell'indirizzo ip del chiamante (via trasporto): "+e.getMessage(),e);
  127.             }
  128.         }
  129.     }
  130.     private static String getIPClientAddressFromHeader(List<String> headers, ConnectorInMessage req) throws ConnectorException{
  131.         if(headers.size()>0){
  132.             for (String header : headers) {
  133.                 String transportAddr = TransportUtils.getFirstValue(req.getHeaderValues(header)); // gestisce nell'implementazione il case insensitive
  134.                 if(transportAddr!=null){
  135.                     return transportAddr;
  136.                 }
  137.             }
  138.         }
  139.         return null;
  140.     }
  141.     public static void setInfoInvocation(Transaction tr, RequestInfo requestInfo, ConnectorInMessage req) throws Exception {
  142.        
  143.         if(tr==null) {
  144.             throw new Exception("Transaction is null");
  145.         }
  146.        
  147.         tr.setRequestInfo(requestInfo);
  148.        
  149.         if(req.getCredential()!=null) {
  150.             Credenziali credenziali = new Credenziali(req.getCredential());
  151.             String credenzialiFornite = "";
  152.             if(credenziali!=null){
  153.                 credenzialiFornite = credenziali.toString(!Credenziali.SHOW_BASIC_PASSWORD,
  154.                         Credenziali.SHOW_ISSUER,
  155.                         !Credenziali.SHOW_DIGEST_CLIENT_CERT,
  156.                         Credenziali.SHOW_SERIAL_NUMBER_CLIENT_CERT,
  157.                         "","","\n"); // riporto anche l'issuer ed il serial number del cert e formatto differentemente
  158.             }
  159.             tr.setCredenziali(credenzialiFornite);
  160.         }
  161.        
  162.         if(req.getURLProtocolContext()!=null){
  163.             String urlInvocazione = req.getURLProtocolContext().getUrlInvocazione_formBased();
  164.             if(req.getURLProtocolContext().getFunction()!=null){
  165.                 urlInvocazione = "["+req.getURLProtocolContext().getFunction()+"] "+urlInvocazione;
  166.             }
  167.             //System.out.println("SET URL INVOCAZIONE ["+urlInvocazione+"]");
  168.             tr.setUrlInvocazione(urlInvocazione);
  169.         }
  170.     }
  171. }