001/*
002 * (C) Copyright 2006-2009 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 *     Nuxeo
018 */
019
020package org.nuxeo.ecm.platform.publisher.remoting.restHandler;
021
022import java.util.Map;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.POST;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.Produces;
029import javax.ws.rs.core.Response;
030
031import org.nuxeo.ecm.core.api.DocumentLocation;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.platform.publisher.api.PublicationNode;
034import org.nuxeo.ecm.platform.publisher.api.PublishedDocument;
035import org.nuxeo.ecm.platform.publisher.api.RemotePublicationTreeManager;
036import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
037import org.nuxeo.runtime.api.Framework;
038
039@Path("remotepublisher")
040@Produces("nuxeo/remotepub;charset=UTF-8")
041public class RestPublishingHandler extends DefaultObject {
042
043    protected RemotePublicationTreeManager getPublisher() {
044        return Framework.getLocalService(RemotePublicationTreeManager.class);
045    }
046
047    @POST
048    @Path("getChildrenDocuments")
049    public RemotePubResult getChildrenDocuments(RemotePubParam param) {
050        return new RemotePubResult(getPublisher().getChildrenDocuments(param.getAsNode()));
051    }
052
053    @POST
054    @Path("getChildrenNodes")
055    public RemotePubResult getChildrenNodes(RemotePubParam param) {
056        return new RemotePubResult(getPublisher().getChildrenNodes(param.getAsNode()));
057    }
058
059    @POST
060    @Path("getParent")
061    public RemotePubResult getParent(RemotePubParam param) {
062        return new RemotePubResult(getPublisher().getParent(param.getAsNode()));
063    }
064
065    @POST
066    @Path("getNodeByPath")
067    public RemotePubResult getNodeByPath(RemotePubParam param) {
068        return new RemotePubResult(getPublisher().getNodeByPath((String) param.getParams().get(0),
069                (String) param.getParams().get(1)));
070    }
071
072    @POST
073    @Path("getExistingPublishedDocument")
074    public RemotePubResult getExistingPublishedDocument(RemotePubParam param) {
075        return new RemotePubResult(getPublisher().getExistingPublishedDocument((String) param.getParams().get(0),
076                (DocumentLocation) param.getParams().get(1)));
077    }
078
079    @POST
080    @Path("getPublishedDocumentInNode")
081    public RemotePubResult getPublishedDocumentInNode(RemotePubParam param) {
082        return new RemotePubResult(getPublisher().getPublishedDocumentInNode(param.getAsNode()));
083    }
084
085    @POST
086    @Path("publish")
087    public RemotePubResult publish(RemotePubParam param) {
088        RemotePubResult result;
089        if (param.getParams().size() == 2) {
090            result = new RemotePubResult(getPublisher().publish((DocumentModel) param.getParams().get(0),
091                    (PublicationNode) param.getParams().get(1)));
092        } else {
093            result = new RemotePubResult(getPublisher().publish((DocumentModel) param.getParams().get(0),
094                    (PublicationNode) param.getParams().get(1), (Map<String, String>) param.getParams().get(2)));
095        }
096        return result;
097    }
098
099    @POST
100    @Path("unpublish")
101    public void unpublish(RemotePubParam param) {
102        Object object = param.getParams().get(1);
103        if (object instanceof PublicationNode) {
104            getPublisher().unpublish((DocumentModel) param.getParams().get(0), (PublicationNode) object);
105        } else if (object instanceof PublishedDocument) {
106            getPublisher().unpublish((String) param.getParams().get(0), (PublishedDocument) object);
107        }
108    }
109
110    @POST
111    @Path("initRemoteSession")
112    public RemotePubResult initRemoteSession(RemotePubParam param) {
113        return new RemotePubResult(getPublisher().initRemoteSession((String) param.getParams().get(0),
114                (Map<String, String>) param.getParams().get(1)));
115    }
116
117    @POST
118    @Path("release")
119    public Response release(RemotePubParam param) {
120        getPublisher().release((String) param.getParams().get(0));
121        return Response.ok().build();
122    }
123
124    @GET
125    @Path("release/{sid}")
126    public Response release(@PathParam("sid") String sid) {
127        getPublisher().release(sid);
128        return Response.ok().build();
129    }
130
131    @GET
132    @Path("ping")
133    public String ping() {
134        return "pong";
135    }
136
137    @GET
138    @Path("superPing")
139    public RemotePubResult superPing() {
140        return new RemotePubResult("pong");
141    }
142
143}