001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model.impl;
023
024import java.text.ParseException;
025import java.util.Arrays;
026import java.util.HashSet;
027
028import org.nuxeo.ecm.webengine.WebEngine;
029import org.nuxeo.ecm.webengine.WebException;
030import org.nuxeo.ecm.webengine.loader.ClassProxy;
031import org.nuxeo.ecm.webengine.model.Access;
032import org.nuxeo.ecm.webengine.model.WebObject;
033import org.nuxeo.ecm.webengine.security.PermissionService;
034import org.nuxeo.ecm.webengine.security.guards.And;
035import org.nuxeo.ecm.webengine.security.guards.IsAdministratorGuard;
036import org.nuxeo.runtime.annotations.AnnotationManager;
037
038/**
039 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
040 */
041public class ResourceTypeImpl extends AbstractResourceType {
042
043    public ResourceTypeImpl(WebEngine engine, ModuleImpl module, ResourceTypeImpl superType, String name,
044            ClassProxy clazz, int visibility) {
045        super(engine, module, superType, name, clazz, visibility);
046    }
047
048    @Override
049    protected void loadAnnotations(AnnotationManager annoMgr) {
050        Class<?> c = clazz.get();
051        WebObject wo = c.getAnnotation(WebObject.class);
052        if (wo == null) {
053            return;
054        }
055        String g = wo.guard();
056        if (g != null && g.length() > 0) {
057            try {
058                guard = PermissionService.parse(g);
059            } catch (ParseException e) {
060                throw WebException.wrap("Failed to parse guard: " + g + " on WebObject " + c.getName(), e);
061            }
062        } else {
063            loadGuardFromAnnoation(c);
064        }
065        Access requireAdministrators = wo.administrator();
066        if (requireAdministrators != Access.NULL) {
067            if (guard != null) {
068                guard = new And(new IsAdministratorGuard(requireAdministrators), guard);
069            } else {
070                guard = new IsAdministratorGuard(requireAdministrators);
071            }
072        }
073        String[] facets = wo.facets();
074        if (facets != null && facets.length > 0) {
075            this.facets = new HashSet<String>(Arrays.asList(facets));
076        }
077    }
078
079}