BaseImpl.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.utils.service;

  21. import java.lang.reflect.Method;

  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24. import javax.ws.rs.Path;
  25. import javax.ws.rs.core.Context;
  26. import javax.ws.rs.core.UriInfo;

  27. import org.openspcoop2.utils.service.context.ContextThreadLocal;
  28. import org.openspcoop2.utils.service.context.IContext;
  29. import org.slf4j.Logger;

  30. /**
  31.  * BaseImpl
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class BaseImpl {

  38.     @Context
  39.     protected HttpServletRequest servletRequest;
  40.     @Context
  41.     protected HttpServletResponse servletResponse;
  42.     @Context
  43.     protected UriInfo uriInfo;
  44.    
  45.     protected Logger log;
  46.    
  47.     public BaseImpl(Logger log){
  48.         this.log = log;
  49.     }
  50.    
  51.     protected synchronized IContext getContext() {
  52.         IContext context = ContextThreadLocal.get();
  53.         if(context instanceof org.openspcoop2.utils.service.context.Context) {
  54.             ((org.openspcoop2.utils.service.context.Context)context).update(this.servletRequest, this.servletResponse, this.uriInfo, 2, this.log);
  55.             ((org.openspcoop2.utils.service.context.Context)context).setRestPath(this.getPathFromRestMethod(context.getMethodName()));
  56.         }
  57.         return context;
  58.     }

  59.     private String getPathFromRestMethod(String methodName) {

  60.         try {
  61.             Class<?> c = this.getClass();
  62.             Class<?> [] interfaces = c.getInterfaces();
  63.             if(interfaces==null || interfaces.length<=0) {
  64.                 return null;
  65.             }
  66.             Class<?> cInterface = null;
  67.             for (int i = 0; i < interfaces.length; i++) {
  68.                 if (interfaces[i] != null && interfaces[i].isAnnotationPresent(Path.class)) {
  69.                     cInterface = interfaces[i];
  70.                     break;
  71.                 }
  72.             }
  73.             if(cInterface==null) {
  74.                 return null;
  75.             }
  76.             Method [] methods = cInterface.getMethods();
  77.             if(methods==null || methods.length<=0) {
  78.                 return null;
  79.             }
  80.             Method method = null;
  81.             for (int i = 0; i < methods.length; i++) {
  82.                 if (methods[i] != null && methods[i].getName().equals(methodName) && methods[i].isAnnotationPresent(Path.class)) {
  83.                     method = methods[i];
  84.                     break;
  85.                 }
  86.             }
  87.             if(method==null) {
  88.                 return null;
  89.             }
  90.             Path path = method.getAnnotation(Path.class);
  91.             if(path==null) {
  92.                 return null;
  93.             }
  94.             return path.value();
  95.         } catch (Exception e) {
  96.             this.log.error(e.getMessage(),e);
  97.         }

  98.         return null;
  99.     }
  100.    
  101. }