changeset 94:fcb6fe10e08c

added config option for webapp URL prefix.
author casties
date Tue, 10 Feb 2015 17:45:56 +0100
parents cf44d9e1a4a7
children acd44dfec9c8
files src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/UrlPrefixFilter.java src/main/webapp/WEB-INF/serverconfig.property.template
diffstat 6 files changed, 108 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java	Sun Feb 08 18:09:00 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java	Tue Feb 10 17:45:56 2015 +0100
@@ -29,6 +29,8 @@
 import org.restlet.engine.application.CorsFilter;
 import org.restlet.routing.Router;
 
+import de.mpiwg.itgroup.annotations.restlet.utils.UrlPrefixFilter;
+
 /**
  * @author casties
  *
@@ -45,6 +47,7 @@
     @Override
     public Restlet createInboundRoot() {
 
+    	Restlet root = null;
         Router router = new Router(getContext());
         
         router.attach("/annotations", AnnotatorAnnotations.class);
@@ -58,18 +61,27 @@
         router.attach("/resources/{id}", AnnotatorResources.class);
         router.attach("/resources/{id}/annotations", AnnotatorAnnotationsByResources.class);
         router.attach("/", AnnotatorInfo.class);
-        //return router;
+        root = router;
 
         // this.authenticator = createAuthenticator();
         // authenticator.setNext(router);
-        // return authenticator;
+        
+        if (this.webappUriPrefix != null) {
+	        // add prefix path to url
+	        UrlPrefixFilter prefixFilter = new UrlPrefixFilter();
+	        prefixFilter.setPrefix(this.webappUriPrefix);;
+	        prefixFilter.setNext(root);
+	        root = prefixFilter;
+        }
 
         // handle Cross-Origin-Resource-Security headers
         CorsFilter corsFilter = new CorsFilter(getContext(), router);
         corsFilter.setAllowedOrigins(new HashSet<String>(Arrays.asList("*")));
         corsFilter.setAllowedCredentials(true);
-        corsFilter.setNext(router);
-        return corsFilter;
+        corsFilter.setNext(root);
+        root = corsFilter;
+
+        return root;
 
     }
 
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java	Sun Feb 08 18:09:00 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/BaseRestlet.java	Tue Feb 10 17:45:56 2015 +0100
@@ -114,6 +114,12 @@
     public static final String AUTHORIZATION_MODE_KEY = "annotationmanager.authorization";
     
     /**
+     * prefix for webapp URLs (if needed by proxying).
+     */
+    protected String webappUriPrefix = null;
+    public static final String WEBAPP_PREFIX = "annotationmanager.webapp.prefix";
+    
+    /**
      * prefix to create uris for tags in store.
      */
     public static String TAGS_URI_PREFIX = "";
@@ -171,6 +177,7 @@
                          */
                         graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath);
                         ldapServerUrl = serverConfig.getProperty(LDAP_SERVER_KEY, null);
+                        webappUriPrefix = serverConfig.getProperty(WEBAPP_PREFIX, null);
                         /*
                          * uri prefixes
                          */
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java	Sun Feb 08 18:09:00 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java	Tue Feb 10 17:45:56 2015 +0100
@@ -48,7 +48,7 @@
  */
 public class AnnotationsResource extends ServerResource {
 
-    public static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.restlet.annotations_ui.AnnotationsResource");
+    public static Logger logger = Logger.getLogger(AnnotationsResource.class.getCanonicalName());
 
     private AnnotationStore store;
 
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java	Sun Feb 08 18:09:00 2015 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java	Tue Feb 10 17:45:56 2015 +0100
@@ -34,6 +34,7 @@
 import org.restlet.security.MapVerifier;
 
 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
+import de.mpiwg.itgroup.annotations.restlet.utils.UrlPrefixFilter;
 
 /**
  * @author casties
@@ -53,9 +54,9 @@
     @Override
     public Restlet createInboundRoot() {
         // this.authenticator = createAuthenticator();
-
+    	Restlet root = null;
+    	
         Router router = new Router(getContext());
-
         router.attach("/groups", GroupsResource.class);
         router.attach("/groups/", GroupsResource.class);
         router.attach("/groups/{id}", GroupResource.class);
@@ -69,8 +70,16 @@
         router.attach("/annotations/", AnnotationsResource.class);
         router.attach("/annotations/{id}", AnnotationResource.class);
         router.attach("/annotations/{id}/", AnnotationResource.class);
-
         router.attach("/", InfoResource.class);
+        root = router;
+        
+        if (this.webappUriPrefix != null) {
+	        // add prefix path to url
+	        UrlPrefixFilter prefixFilter = new UrlPrefixFilter();
+	        prefixFilter.setPrefix(this.webappUriPrefix);;
+	        prefixFilter.setNext(root);
+	        root = prefixFilter;
+        }
 
         // use simple password verifier
         MapVerifier verifier = new MapVerifier();
@@ -86,8 +95,10 @@
         ChallengeAuthenticator guard = new ChallengeAuthenticator(getContext(), ChallengeScheme.HTTP_BASIC, "Tutorial");
         guard.setVerifier(verifier);
         // put everything through guard
-        guard.setNext(router);
-        return guard;
+        guard.setNext(root);
+        root = guard;
+        
+        return root;
     }
 
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/UrlPrefixFilter.java	Tue Feb 10 17:45:56 2015 +0100
@@ -0,0 +1,67 @@
+package de.mpiwg.itgroup.annotations.restlet.utils;
+
+/*
+ * #%L
+ * AnnotationManager
+ * %%
+ * Copyright (C) 2012 - 2015 MPIWG Berlin
+ * %%
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as 
+ * published by the Free Software Foundation, either version 3 of the 
+ * License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Lesser Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Lesser Public 
+ * License along with this program.  If not, see
+ * <http://www.gnu.org/licenses/lgpl-3.0.html>.
+ * #L%
+ */
+
+import org.restlet.Request;
+import org.restlet.Response;
+import org.restlet.data.Reference;
+import org.restlet.routing.Filter;
+
+/**
+ * Filter that adds a prefix path to all internally generated URLs.
+ * 
+ * @author casties
+ *
+ */
+public class UrlPrefixFilter extends Filter {
+
+	private String prefix = null;
+	
+	/**
+	 * Set the prefix to add.
+	 * 
+	 * @param prefix
+	 */
+	public void setPrefix(String prefix) {
+		if (!prefix.startsWith("/")) {
+			prefix = "/" + prefix;
+		}
+		this.prefix = prefix;
+	}
+	
+	@Override
+	protected int beforeHandle(Request request, Response response) {
+		// get ref to change
+	    Reference ref = request.getResourceRef();
+	    String path = ref.getPath();
+	    // add prefix to path
+	    ref.setPath(prefix + path);
+	    // change baseRef as well
+	    Reference baseRef = ref.getBaseRef();
+	    String basePath = baseRef.getPath();
+	    // add prefix to base ref path
+	    baseRef.setPath(prefix + basePath);
+		return super.beforeHandle(request, response);
+	}
+
+}
--- a/src/main/webapp/WEB-INF/serverconfig.property.template	Sun Feb 08 18:09:00 2015 +0100
+++ b/src/main/webapp/WEB-INF/serverconfig.property.template	Tue Feb 10 17:45:56 2015 +0100
@@ -9,3 +9,4 @@
 annotationmanager.uris.persons = http://entities.mpiwg-berlin.mpg.de/persons/
 annotationmanager.uris.groups = http://entities.mpiwg-berlin.mpg.de/groups/
 annotationmanager.uris.annotations = http://entities.mpiwg-berlin.mpg.de/annotations/
+#annotationmanager.webapp.prefix = /annotations