fix for #4844
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / util / CdmVaadinAuthentication.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.vaadin.util;
11
12 import java.net.URI;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.security.core.Authentication;
18
19 /**
20 * @author cmathew
21 * @date 28 Apr 2015
22 *
23 */
24 public class CdmVaadinAuthentication {
25 private final static Logger logger = Logger.getLogger(CdmVaadinAuthentication.class);
26
27 public static final String KEY = "key_authentication";
28
29 Map<String, Authentication> hostAuthenticationMap = new HashMap<String, Authentication>();
30
31 public void addAuthentication(URI requestSourceUri, String requestSourceContext, Authentication authentication) {
32 addAuthentication(getRequestSource(requestSourceUri, requestSourceContext), authentication);
33 }
34
35 public void addAuthentication(String requestSource, Authentication authentication) {
36 if(requestSource == null || requestSource.isEmpty()) {
37 throw new IllegalStateException("When setting authentication, host cannot be null or empty");
38 }
39
40 if(authentication == null) {
41 throw new IllegalStateException("When setting authentication, authentication object cannot be null");
42 }
43 hostAuthenticationMap.put(requestSource, authentication);
44 }
45
46 public boolean isAuthenticated(URI uri, String context) {
47 if(uri != null && context != null && !context.isEmpty()) {
48 Authentication authentication = hostAuthenticationMap.get(getRequestSource(uri, context));
49 if(authentication != null) {
50 return authentication.isAuthenticated();
51 }
52 }
53 return false;
54 }
55
56 public static String getRequestSource(URI uri, String context) {
57 String source = uri.getHost() + ":" + String.valueOf(uri.getPort()) + context;
58 logger.warn(" request source : " + source);
59 return source;
60 }
61
62 }