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

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

/**
 * 
 */
package de.mpiwg.itgroup.annotations.restlet.annotations_ui;

/*
 * #%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.logging.Logger;

import org.restlet.Restlet;
import org.restlet.data.ChallengeScheme;
import org.restlet.routing.Router;
import org.restlet.security.ChallengeAuthenticator;
import org.restlet.security.MapVerifier;

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

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

    public final String version = "AnnotationManagerN4J/AnnotationsUI 0.3";

    public static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.restlet.annotations_ui.AnnotationsUiRestlet");

    /*
     * (non-Javadoc)
     * 
     * @see org.restlet.Application#createInboundRoot()
     */
    @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);
        router.attach("/groups/{id}/", GroupResource.class);
        router.attach("/groups/{id}/members", GroupMembersResource.class);
        router.attach("/persons", PersonsResource.class);
        router.attach("/persons/", PersonsResource.class);
        router.attach("/persons/{id}", PersonResource.class);
        router.attach("/persons/{id}/", PersonResource.class);
        router.attach("/annotations", AnnotationsResource.class);
        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();
        // get user name and password
        String user = serverConfig.getProperty(BaseRestlet.ADMIN_USER_KEY);
        String pw = serverConfig.getProperty(BaseRestlet.ADMIN_PASSWORD_KEY);
        if (user != null && pw != null) {
            verifier.getLocalSecrets().put(user, pw.toCharArray());
        } else {
            logger.severe("Admin user and password missing in serverconfig!");
        }
        // Create a Guard
        ChallengeAuthenticator guard = new ChallengeAuthenticator(getContext(), ChallengeScheme.HTTP_BASIC, "Tutorial");
        guard.setVerifier(verifier);
        // put everything through guard
        guard.setNext(root);
        root = guard;
        
        return root;
    }

    @Override
    public String getVersion() {
        return version;
    }

}