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