| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.filter; |
| 2 | |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 3 | import java.security.SecureRandom; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.List; |
| 7 | import java.util.Locale; |
| 8 | import java.util.Map; |
| 9 | import java.util.Random; |
| 10 | |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 11 | import org.apache.logging.log4j.LogManager; |
| 12 | import org.apache.logging.log4j.Logger; |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 13 | import org.glassfish.jersey.client.ClientConfig; |
| 14 | import org.glassfish.jersey.logging.LoggingFeature; |
| margaretha | b156f88 | 2023-05-19 14:01:03 +0200 | [diff] [blame] | 15 | import org.springframework.beans.factory.annotation.Autowired; |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 16 | |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 17 | import de.ids_mannheim.korap.authentication.AuthenticationManager; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 18 | import jakarta.ws.rs.client.Client; |
| 19 | import jakarta.ws.rs.client.ClientBuilder; |
| 20 | import jakarta.ws.rs.client.WebTarget; |
| 21 | import jakarta.ws.rs.container.ContainerRequestContext; |
| 22 | import jakarta.ws.rs.container.ContainerRequestFilter; |
| 23 | import jakarta.ws.rs.core.UriBuilder; |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 24 | import net.minidev.json.JSONArray; |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * @author hanl |
| 28 | * @date 13/05/2014 |
| 29 | */ |
| margaretha | b156f88 | 2023-05-19 14:01:03 +0200 | [diff] [blame] | 30 | @Deprecated |
| 31 | //@Component |
| 32 | //@Priority(Priorities.AUTHORIZATION) |
| abcpro1 | 136ff59 | 2022-11-07 18:25:03 +0000 | [diff] [blame] | 33 | public class PiwikFilter implements ContainerRequestFilter { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 34 | |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 35 | private WebTarget service; |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 36 | // private static final String SERVICE = "http://localhost:8888"; |
| 37 | private static final String SERVICE = "http://10.0.10.13"; |
| margaretha | 49cb688 | 2018-07-04 04:19:54 +0200 | [diff] [blame] | 38 | private static Logger jlog = LogManager.getLogger(PiwikFilter.class); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 39 | public static boolean ENABLED = false; |
| 40 | private Map<String, String> customVars; |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 41 | @Autowired |
| margaretha | 3495447 | 2018-10-24 20:05:17 +0200 | [diff] [blame] | 42 | private AuthenticationManager authenticationManager; |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 43 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 44 | public PiwikFilter () { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 45 | // controller = BeansFactory.getKustvaktContext() |
| 46 | // .getAuthenticationManager(); |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 47 | ClientConfig clientConfig = new ClientConfig(); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 48 | if (jlog.isDebugEnabled()) |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 49 | clientConfig.register(LoggingFeature.class); |
| 50 | Client client = ClientBuilder.newClient(clientConfig); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 51 | UriBuilder b = UriBuilder.fromUri(SERVICE); |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 52 | service = client.target(b.build()); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 53 | this.customVars = new HashMap<>(); |
| 54 | } |
| 55 | |
| abcpro1 | 136ff59 | 2022-11-07 18:25:03 +0000 | [diff] [blame] | 56 | private void send (ContainerRequestContext request) { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 57 | Random random = new SecureRandom(); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 58 | Locale l = null; |
| 59 | if (request.getAcceptableLanguages() != null) |
| 60 | l = request.getAcceptableLanguages().get(0); |
| 61 | try { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 62 | service.path("piwik/piwik.php").queryParam("idsite", "2") |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame] | 63 | .queryParam("rec", "1") |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 64 | //todo check for empty container |
| 65 | .queryParam("_cvar", translateCustomData()) |
| abcpro1 | a94a042 | 2022-11-07 20:07:23 +0000 | [diff] [blame] | 66 | .queryParam("cip", request.getHeaderString("Host")) |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 67 | .queryParam("cookie", "false") |
| 68 | .queryParam("r", String.valueOf(random.nextDouble())) |
| 69 | .queryParam("action_name", |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 70 | request.getUriInfo().getRequestUri() |
| 71 | .toASCIIString()) |
| 72 | .request().accept("text/html") |
| abcpro1 | a94a042 | 2022-11-07 20:07:23 +0000 | [diff] [blame] | 73 | .header("Host", request.getHeaderString("Host")) |
| 74 | .header("User-Agent", request.getHeaderString("User-Agent")) |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 75 | .acceptLanguage(l).method("GET"); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 76 | } |
| 77 | catch (Exception e) { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 78 | // do nothing if piwik not available! |
| 79 | } |
| 80 | } |
| 81 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 82 | private String translateCustomData () { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 83 | final Map<String, List<String>> customVariables = new HashMap<String, List<String>>(); |
| 84 | int i = 0; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 85 | for (final Map.Entry<String, String> entry : this.customVars |
| 86 | .entrySet()) { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 87 | i++; |
| 88 | final List<String> list = new ArrayList<String>(); |
| 89 | list.add(entry.getKey()); |
| 90 | list.add(entry.getValue()); |
| 91 | customVariables.put(Integer.toString(i), list); |
| 92 | } |
| 93 | |
| 94 | final JSONArray json = new JSONArray(); |
| 95 | json.add(customVariables); |
| 96 | |
| 97 | // remove unnecessary parent square brackets from JSON-string |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 98 | String jsonString = json.toString().substring(1, |
| 99 | json.toString().length() - 1); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 100 | customVars.clear(); |
| 101 | return jsonString; |
| 102 | } |
| 103 | |
| 104 | @Override |
| abcpro1 | 136ff59 | 2022-11-07 18:25:03 +0000 | [diff] [blame] | 105 | public void filter (ContainerRequestContext request) { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 106 | if (ENABLED) { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame^] | 107 | // try { |
| 108 | // TokenContext context; |
| 109 | // SecurityContext securityContext = request.getSecurityContext(); |
| 110 | // if (securityContext != null) { |
| 111 | // context = (TokenContext) securityContext.getUserPrincipal(); |
| 112 | // |
| 113 | // if (context.getUsername() != null){ |
| 114 | // // since this is cached, not very expensive! |
| 115 | // User user = authenticationManager.getUser(context.getUsername()); |
| 116 | // Userdata data = authenticationManager |
| 117 | // .getUserData(user, UserSettingProcessor.class); |
| 118 | // if ((Boolean) data.get(Attributes.COLLECT_AUDITING_DATA)) |
| 119 | // customVars.put("username", context.getUsername()); |
| 120 | // } |
| 121 | // } |
| 122 | // } |
| 123 | // catch (KustvaktException e) { |
| 124 | // //do nothing |
| 125 | // } |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 126 | send(request); |
| 127 | } |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 128 | } |
| 129 | } |