| 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 | |
| 45 | public PiwikFilter () { |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 46 | // controller = BeansFactory.getKustvaktContext() |
| 47 | // .getAuthenticationManager(); |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 48 | ClientConfig clientConfig = new ClientConfig(); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 49 | if (jlog.isDebugEnabled()) |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 50 | clientConfig.register(LoggingFeature.class); |
| 51 | Client client = ClientBuilder.newClient(clientConfig); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 52 | UriBuilder b = UriBuilder.fromUri(SERVICE); |
| abcpro1 | d912a2c | 2022-11-07 17:10:50 +0000 | [diff] [blame] | 53 | service = client.target(b.build()); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 54 | this.customVars = new HashMap<>(); |
| 55 | } |
| 56 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 57 | |
| abcpro1 | 136ff59 | 2022-11-07 18:25:03 +0000 | [diff] [blame] | 58 | private void send (ContainerRequestContext request) { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 59 | Random random = new SecureRandom(); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 60 | Locale l = null; |
| 61 | if (request.getAcceptableLanguages() != null) |
| 62 | l = request.getAcceptableLanguages().get(0); |
| 63 | try { |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 64 | service.path("piwik/piwik.php") |
| 65 | .queryParam("idsite", "2") |
| Michael Hanl | 7d92561 | 2016-01-28 16:59:30 +0100 | [diff] [blame] | 66 | .queryParam("rec", "1") |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 67 | //todo check for empty container |
| 68 | .queryParam("_cvar", translateCustomData()) |
| abcpro1 | a94a042 | 2022-11-07 20:07:23 +0000 | [diff] [blame] | 69 | .queryParam("cip", request.getHeaderString("Host")) |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 70 | .queryParam("cookie", "false") |
| 71 | .queryParam("r", String.valueOf(random.nextDouble())) |
| 72 | .queryParam("action_name", |
| abcpro1 | 9668724 | 2022-11-07 20:00:19 +0000 | [diff] [blame] | 73 | request.getUriInfo().getRequestUri().toASCIIString()) |
| abcpro1 | 241bc4f | 2022-11-07 20:13:57 +0000 | [diff] [blame] | 74 | .request() |
| abcpro1 | 8e5ff51 | 2022-11-07 17:27:13 +0000 | [diff] [blame] | 75 | .accept("text/html") |
| abcpro1 | a94a042 | 2022-11-07 20:07:23 +0000 | [diff] [blame] | 76 | .header("Host", request.getHeaderString("Host")) |
| 77 | .header("User-Agent", request.getHeaderString("User-Agent")) |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 78 | .acceptLanguage(l).method("GET"); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 79 | } |
| 80 | catch (Exception e) { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 81 | // do nothing if piwik not available! |
| 82 | } |
| 83 | } |
| 84 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 85 | |
| 86 | private String translateCustomData () { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 87 | final Map<String, List<String>> customVariables = new HashMap<String, List<String>>(); |
| 88 | int i = 0; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 89 | for (final Map.Entry<String, String> entry : this.customVars.entrySet()) { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 90 | i++; |
| 91 | final List<String> list = new ArrayList<String>(); |
| 92 | list.add(entry.getKey()); |
| 93 | list.add(entry.getValue()); |
| 94 | customVariables.put(Integer.toString(i), list); |
| 95 | } |
| 96 | |
| 97 | final JSONArray json = new JSONArray(); |
| 98 | json.add(customVariables); |
| 99 | |
| 100 | // remove unnecessary parent square brackets from JSON-string |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 101 | String jsonString = json.toString().substring(1, |
| 102 | json.toString().length() - 1); |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 103 | customVars.clear(); |
| 104 | return jsonString; |
| 105 | } |
| 106 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 107 | |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 108 | @Override |
| abcpro1 | 136ff59 | 2022-11-07 18:25:03 +0000 | [diff] [blame] | 109 | public void filter (ContainerRequestContext request) { |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 110 | if (ENABLED) { |
| margaretha | b156f88 | 2023-05-19 14:01:03 +0200 | [diff] [blame] | 111 | // try { |
| 112 | // TokenContext context; |
| 113 | // SecurityContext securityContext = request.getSecurityContext(); |
| 114 | // if (securityContext != null) { |
| 115 | // context = (TokenContext) securityContext.getUserPrincipal(); |
| 116 | // |
| 117 | // if (context.getUsername() != null){ |
| 118 | // // since this is cached, not very expensive! |
| 119 | // User user = authenticationManager.getUser(context.getUsername()); |
| 120 | // Userdata data = authenticationManager |
| 121 | // .getUserData(user, UserSettingProcessor.class); |
| 122 | // if ((Boolean) data.get(Attributes.COLLECT_AUDITING_DATA)) |
| 123 | // customVars.put("username", context.getUsername()); |
| 124 | // } |
| 125 | // } |
| 126 | // } |
| 127 | // catch (KustvaktException e) { |
| 128 | // //do nothing |
| 129 | // } |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 130 | send(request); |
| 131 | } |
| Michael Hanl | 50f9e52 | 2015-09-25 12:38:17 +0200 | [diff] [blame] | 132 | } |
| 133 | } |