view src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java @ 94:fcb6fe10e08c

added config option for webapp URL prefix.
author casties
date Tue, 10 Feb 2015 17:45:56 +0100
parents cf44d9e1a4a7
children e953327d66bb
line wrap: on
line source

package de.mpiwg.itgroup.annotations.restlet;

/*
 * #%L
 * AnnotationManager
 * %%
 * Copyright (C) 2012 - 2014 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 java.util.Arrays;
import java.util.HashSet;

import org.restlet.Restlet;
import org.restlet.engine.application.CorsFilter;
import org.restlet.routing.Router;

import de.mpiwg.itgroup.annotations.restlet.utils.UrlPrefixFilter;

/**
 * @author casties
 *
 */
public class AnnotatorRestlet extends BaseRestlet {

    public final String version = "AnnotationManagerN4J/Annotator 0.5.1";

    /*
     * (non-Javadoc)
     * 
     * @see org.restlet.Application#createInboundRoot()
     */
    @Override
    public Restlet createInboundRoot() {

    	Restlet root = null;
        Router router = new Router(getContext());
        
        router.attach("/annotations", AnnotatorAnnotations.class);
        router.attach("/annotations/{id}", AnnotatorAnnotations.class);
        router.attach("/search", AnnotatorSearch.class);
        router.attach("/groups", AnnotatorGroups.class);
        router.attach("/tags", AnnotatorTags.class);
        router.attach("/tags/{id}", AnnotatorTags.class);
        router.attach("/tags/{id}/annotations", AnnotatorAnnotationsByTags.class);
        router.attach("/resources", AnnotatorResources.class);
        router.attach("/resources/{id}", AnnotatorResources.class);
        router.attach("/resources/{id}/annotations", AnnotatorAnnotationsByResources.class);
        router.attach("/", AnnotatorInfo.class);
        root = router;

        // this.authenticator = createAuthenticator();
        // authenticator.setNext(router);
        
        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(root);
        root = corsFilter;

        return root;

    }

    /* (non-Javadoc)
     * @see de.mpiwg.itgroup.annotations.restlet.RestletImpl#getVersion()
     */
    @Override
    public String getVersion() {
        return version;
    }

}