001/*
002 * (C) Copyright 2014 Nuxeo SA (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-2.1.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 *     vpasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.test.helpers;
018
019import javax.servlet.http.HttpServletResponse;
020
021import org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Context;
023import org.nuxeo.ecm.automation.core.annotations.Operation;
024import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
025import org.nuxeo.ecm.automation.core.annotations.Param;
026import org.nuxeo.ecm.automation.jaxrs.io.operations.RestOperationContext;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.PathRef;
030
031/**
032 * @since 7.1
033 */
034@Operation(id = HttpStatusOperationTest.ID, category = Constants.CAT_SERVICES,
035        label = "Test Custom Http Status", description = "Test Custom Http " +
036        "Status")
037public class HttpStatusOperationTest {
038
039    public static final String ID = "Test.HttpStatus";
040
041    @Param(name = "isFailing")
042    protected boolean isFailing = true;
043
044    @Context
045    RestOperationContext context;
046
047    @Context
048    CoreSession session;
049
050    @OperationMethod()
051    public Object run() throws Exception {
052        DocumentModel root = session.getDocument(new PathRef("/"));
053        // If context is instanceof RestOperationContext when jaxrs call is
054        // executed
055        if (context != null) {
056            if (isFailing) {
057                ExceptionTest exception = new ExceptionTest("Exception " +
058                        "Message");
059                exception.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
060                throw exception;
061            } else {
062                context.setHttpStatus(HttpServletResponse
063                        .SC_PARTIAL_CONTENT);
064            }
065        }// else context is instanceof OperationContext
066        return root;
067    }
068
069}