001/* 002 * (C) Copyright 2006-2016 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 * vpasquier 019 * slacoin 020 */ 021package org.nuxeo.ecm.automation.server; 022 023import java.security.Principal; 024import java.util.ArrayList; 025import java.util.HashMap; 026import java.util.List; 027import java.util.Map; 028 029import javax.servlet.http.HttpServletRequest; 030import javax.ws.rs.ext.MessageBodyReader; 031import javax.ws.rs.ext.MessageBodyWriter; 032 033import org.nuxeo.ecm.automation.core.Constants; 034import org.nuxeo.ecm.automation.io.services.IOComponent; 035import org.nuxeo.ecm.core.api.NuxeoPrincipal; 036import org.nuxeo.runtime.api.Framework; 037import org.nuxeo.runtime.model.ComponentContext; 038import org.nuxeo.runtime.model.ComponentInstance; 039import org.nuxeo.runtime.model.DefaultComponent; 040 041/** 042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 043 */ 044public class AutomationServerComponent extends DefaultComponent implements AutomationServer { 045 046 protected static final String XP_BINDINGS = "bindings"; 047 048 protected static final String IOCOMPONENT_NAME = "org.nuxeo.ecm.automation.io.services.IOComponent"; 049 050 protected IOComponent ioComponent; 051 052 private static final String XP_MARSHALLER = "marshallers"; 053 054 protected Map<String, RestBinding> bindings; 055 056 protected static final String XP_CODECS = "codecs"; 057 058 protected volatile Map<String, RestBinding> lookup; 059 060 protected List<Class<? extends MessageBodyWriter<?>>> writers; 061 062 protected List<Class<? extends MessageBodyReader<?>>> readers; 063 064 @Override 065 public void activate(ComponentContext context) { 066 bindings = new HashMap<>(); 067 writers = new ArrayList<>(); 068 readers = new ArrayList<>(); 069 ioComponent = ((IOComponent) Framework.getRuntime().getComponentInstance(IOCOMPONENT_NAME).getInstance()); 070 } 071 072 @Override 073 public void deactivate(ComponentContext context) { 074 bindings = null; 075 } 076 077 @Override 078 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 079 if (XP_BINDINGS.equals(extensionPoint)) { 080 RestBinding binding = (RestBinding) contribution; 081 addBinding(binding); 082 } else if (XP_MARSHALLER.equals(extensionPoint)) { 083 MarshallerDescriptor marshaller = (MarshallerDescriptor) contribution; 084 writers.addAll(marshaller.getWriters()); 085 readers.addAll(marshaller.getReaders()); 086 } else if (XP_CODECS.equals(extensionPoint)) { 087 ioComponent.registerContribution(contribution, extensionPoint, contributor); 088 } 089 } 090 091 @Override 092 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 093 if (XP_BINDINGS.equals(extensionPoint)) { 094 RestBinding binding = (RestBinding) contribution; 095 removeBinding(binding); 096 } else if (XP_CODECS.equals(extensionPoint)) { 097 ioComponent.unregisterContribution(contribution, extensionPoint, contributor); 098 } 099 } 100 101 @Override 102 public <T> T getAdapter(Class<T> adapter) { 103 if (AutomationServer.class.isAssignableFrom(adapter)) { 104 return adapter.cast(this); 105 } 106 return null; 107 } 108 109 @Override 110 public void applicationStarted(ComponentContext context) { 111 super.applicationStarted(context); 112 } 113 114 @Override 115 public RestBinding getOperationBinding(String name) { 116 return lookup().get(name); 117 } 118 119 @Override 120 public RestBinding getChainBinding(String name) { 121 return lookup().get(Constants.CHAIN_ID_PREFIX + name); 122 } 123 124 @Override 125 public RestBinding[] getBindings() { 126 Map<String, RestBinding> map = lookup(); 127 return map.values().toArray(new RestBinding[map.size()]); 128 } 129 130 protected String getBindingKey(RestBinding binding) { 131 return binding.isChain() ? Constants.CHAIN_ID_PREFIX + binding.getName() : binding.getName(); 132 } 133 134 @Override 135 public synchronized void addBinding(RestBinding binding) { 136 String key = getBindingKey(binding); 137 bindings.put(key, binding); 138 lookup = null; 139 } 140 141 @Override 142 public synchronized RestBinding removeBinding(RestBinding binding) { 143 RestBinding result = bindings.remove(getBindingKey(binding)); 144 lookup = null; 145 return result; 146 } 147 148 @Override 149 public boolean accept(String name, boolean isChain, HttpServletRequest req) { 150 if (isChain) { 151 name = Constants.CHAIN_ID_PREFIX + name; 152 } 153 RestBinding binding = lookup().get(name); 154 if (binding != null) { 155 if (binding.isDisabled()) { 156 return false; 157 } 158 if (binding.isSecure()) { 159 if (!req.isSecure()) { 160 return false; 161 } 162 } 163 Principal principal = req.getUserPrincipal(); 164 165 if (binding.isAdministrator() || binding.hasGroups()) { 166 if (principal instanceof NuxeoPrincipal) { 167 NuxeoPrincipal np = (NuxeoPrincipal) principal; 168 if (binding.isAdministrator() && np.isAdministrator()) { 169 return true; 170 } 171 if (binding.hasGroups()) { 172 for (String group : binding.getGroups()) { 173 if (np.isMemberOf(group)) { 174 return true; 175 } 176 } 177 } 178 } 179 return false; 180 } 181 } 182 return true; 183 } 184 185 private Map<String, RestBinding> lookup() { 186 Map<String, RestBinding> _lookup = lookup; 187 if (_lookup == null) { 188 synchronized (this) { 189 lookup = new HashMap<>(bindings); 190 _lookup = lookup; 191 } 192 } 193 return _lookup; 194 } 195 196 @Override 197 public List<Class<? extends MessageBodyWriter<?>>> getWriters() { 198 return writers; 199 } 200 201 @Override 202 public List<Class<? extends MessageBodyReader<?>>> getReaders() { 203 return readers; 204 } 205 206}