001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.model.impl;
021
022import java.text.ParseException;
023import java.util.Arrays;
024import java.util.HashSet;
025
026import org.nuxeo.ecm.webengine.WebEngine;
027import org.nuxeo.ecm.webengine.WebException;
028import org.nuxeo.ecm.webengine.loader.ClassProxy;
029import org.nuxeo.ecm.webengine.model.Access;
030import org.nuxeo.ecm.webengine.model.WebObject;
031import org.nuxeo.ecm.webengine.security.PermissionService;
032import org.nuxeo.ecm.webengine.security.guards.And;
033import org.nuxeo.ecm.webengine.security.guards.IsAdministratorGuard;
034import org.nuxeo.runtime.annotations.AnnotationManager;
035
036/**
037 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
038 */
039public class ResourceTypeImpl extends AbstractResourceType {
040
041    public ResourceTypeImpl(WebEngine engine, ModuleImpl module, ResourceTypeImpl superType, String name,
042            ClassProxy clazz, int visibility) {
043        super(engine, module, superType, name, clazz, visibility);
044    }
045
046    @Override
047    protected void loadAnnotations(AnnotationManager annoMgr) {
048        Class<?> c = clazz.get();
049        WebObject wo = c.getAnnotation(WebObject.class);
050        if (wo == null) {
051            return;
052        }
053        String g = wo.guard();
054        if (g != null && g.length() > 0) {
055            try {
056                guard = PermissionService.parse(g);
057            } catch (ParseException e) {
058                throw WebException.wrap("Failed to parse guard: " + g + " on WebObject " + c.getName(), e);
059            }
060        } else {
061            loadGuardFromAnnoation(c);
062        }
063        Access requireAdministrators = wo.administrator();
064        if (requireAdministrators != Access.NULL) {
065            if (guard != null) {
066                guard = new And(new IsAdministratorGuard(requireAdministrators), guard);
067            } else {
068                guard = new IsAdministratorGuard(requireAdministrators);
069            }
070        }
071        String[] facets = wo.facets();
072        if (facets != null && facets.length > 0) {
073            this.facets = new HashSet<String>(Arrays.asList(facets));
074        }
075    }
076
077}