diff src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/UrlPrefixFilter.java @ 94:fcb6fe10e08c

added config option for webapp URL prefix.
author casties
date Tue, 10 Feb 2015 17:45:56 +0100
parents
children 8fefa4651d00
line wrap: on
line diff
--- /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);
+	}
+
+}