001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.server.jaxrs;
013
014import javax.ws.rs.GET;
015import javax.ws.rs.Path;
016import javax.ws.rs.Produces;
017
018import org.nuxeo.ecm.automation.AutomationService;
019import org.nuxeo.ecm.automation.OperationContext;
020import org.nuxeo.ecm.automation.OperationException;
021import org.nuxeo.ecm.automation.OperationType;
022import org.nuxeo.ecm.automation.jaxrs.io.operations.ExecutionRequest;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027public class OperationResource extends ExecutableResource {
028
029    protected OperationType type;
030
031    public OperationResource(AutomationService service, OperationType type) {
032        super(service);
033        this.type = type;
034    }
035
036    @GET
037    public Object doGet() throws OperationException {
038        return type.getDocumentation();
039    }
040
041    @GET
042    @Path("yaml")
043    @Produces("application/yaml")
044    public Object doGetYaml() throws OperationException {
045        return type.getDocumentation();
046    }
047
048    @Override
049    public Object execute(ExecutionRequest xreq) throws OperationException {
050        OperationContext ctx = xreq.createContext(request, getCoreSession());
051        return service.run(ctx, type.getId(), xreq.getParams());
052    }
053
054    protected static String entityType(Class<?> clazz) {
055        return clazz.getSimpleName().toLowerCase();
056    }
057
058    @Override
059    public String getId() {
060        return type.getId();
061    }
062
063    @Override
064    public boolean isChain() {
065        return false;
066    }
067
068}