AbstractBasicAuthenticationEntryPoint.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.authentication.entrypoint.jaxrs;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import java.util.List;
  24. import java.util.Set;
  25. import java.util.TimeZone;

  26. import javax.servlet.ServletOutputStream;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import javax.ws.rs.core.MultivaluedMap;
  30. import javax.ws.rs.core.Response;

  31. import org.apache.commons.io.IOUtils;
  32. import org.openspcoop2.utils.Costanti;
  33. import org.openspcoop2.utils.jaxrs.JacksonJsonProviderCustomized;
  34. import org.springframework.security.core.AuthenticationException;
  35. import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;

  36. import com.fasterxml.jackson.databind.ObjectMapper;

  37. /**
  38.  * Problem
  39.  *
  40.  * @author Giuliano Pintori (pintori@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public abstract class AbstractBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {
  45.    
  46.     private String realname = Costanti.OPENSPCOOP2;
  47.    
  48.     public String getRealname() {
  49.         return this.realname;
  50.     }
  51.     public void setRealname(String realname) {
  52.         this.realname = realname;
  53.     }
  54.    
  55.     private TimeZone timeZone = TimeZone.getDefault();
  56.     private String timeZoneId = null;
  57.     public String getTimeZoneId() {
  58.         return this.timeZoneId;
  59.     }
  60.     public void setTimeZoneId(String timeZoneId) {
  61.         this.timeZoneId = timeZoneId;
  62.         this.timeZone = TimeZone.getTimeZone(timeZoneId);
  63.     }
  64.    
  65.     public void fillResponse(AuthenticationException authException, HttpServletResponse httpResponse) {
  66.         AbstractBasicAuthenticationEntryPoint.fillResponse(httpResponse, getPayload(authException, httpResponse), this.timeZone);
  67.     }
  68.    
  69.     public static void fillResponse(HttpServletResponse httpResponse, Response response, TimeZone timeZone) {
  70.         ByteArrayInputStream bais = null;
  71.         ServletOutputStream outputStream = null;
  72.         try{
  73.             httpResponse.setStatus(response.getStatus());

  74.             MultivaluedMap<String, Object> headers = response.getHeaders();
  75.             if(!headers.isEmpty()) {
  76.                 Set<String> keySet = headers.keySet();

  77.                 for (String headerKey : keySet) {
  78.                     List<Object> list = headers.get(headerKey);
  79.                     if(!list.isEmpty()) {
  80.                         StringBuilder sb = new StringBuilder();
  81.                         for (Object object : list) {
  82.                             if(sb.length() > 0)
  83.                                 sb.append(", ");

  84.                             sb.append(object);
  85.                         }
  86.                         httpResponse.setHeader(headerKey, sb.toString());
  87.                     }
  88.                 }
  89.             }

  90.             ObjectMapper mapper = JacksonJsonProviderCustomized.getObjectMapper(false, timeZone);
  91.             String fault = mapper.writeValueAsString(response.getEntity());
  92.             bais = new ByteArrayInputStream(fault.getBytes());

  93.             outputStream = httpResponse.getOutputStream();

  94.             IOUtils.copy(bais, outputStream);

  95.             outputStream.flush();
  96.         }catch(Exception e) {

  97.         } finally {
  98.             if(bais!= null) {
  99.                 try {
  100.                     bais.close();
  101.                 } catch (IOException e) {
  102.                 }
  103.             }
  104.         }
  105.     }
  106.    
  107.     protected abstract Response getPayload(AuthenticationException authException, HttpServletResponse httpResponse);

  108.     protected abstract void addCustomHeaders(javax.servlet.http.HttpServletResponse httpResponse);
  109.    
  110.     @Override
  111.     public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException)  {
  112.         this.addCustomHeaders(response);
  113.         this.fillResponse(authException, response);
  114.     }

  115.     @Override
  116.     public void afterPropertiesSet() {
  117.         setRealmName(this.realname);
  118.         super.afterPropertiesSet();
  119.     }
  120. }