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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.opencmis.tests;
020
021import java.lang.reflect.Method;
022import java.math.BigInteger;
023import java.util.Map;
024
025import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession;
026import org.apache.chemistry.opencmis.client.bindings.spi.http.DefaultHttpInvoker;
027import org.apache.chemistry.opencmis.client.bindings.spi.http.HttpInvoker;
028import org.apache.chemistry.opencmis.client.bindings.spi.http.Output;
029import org.apache.chemistry.opencmis.client.bindings.spi.http.Response;
030import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
031
032/**
033 * HTTP Invoker that notes the last status returned.
034 *
035 * @since 7.1
036 */
037public class StatusLoggingDefaultHttpInvoker implements HttpInvoker {
038
039    public static int lastStatus;
040
041    protected final DefaultHttpInvoker invoker;
042
043    protected Method invokeMethod;
044
045    public StatusLoggingDefaultHttpInvoker() {
046        // we delegate instead of subclassing because the method we're
047        // interested in overriding (invoke) is private...
048        invoker = new DefaultHttpInvoker();
049        for (Method m : invoker.getClass().getDeclaredMethods()) {
050            if (m.getName().equals("invoke")) {
051                invokeMethod = m;
052                invokeMethod.setAccessible(true);
053                break;
054            }
055        }
056    }
057
058    public Response invokeGET(UrlBuilder url, BindingSession session) {
059        return invoke(url, "GET", null, null, null, session, null, null);
060    }
061
062    public Response invokeGET(UrlBuilder url, BindingSession session, BigInteger offset, BigInteger length) {
063        return invoke(url, "GET", null, null, null, session, offset, length);
064    }
065
066    public Response invokePOST(UrlBuilder url, String contentType, Output writer, BindingSession session) {
067        return invoke(url, "POST", contentType, null, writer, session, null, null);
068    }
069
070    public Response invokePUT(UrlBuilder url, String contentType, Map<String, String> headers, Output writer,
071            BindingSession session) {
072        return invoke(url, "PUT", contentType, headers, writer, session, null, null);
073    }
074
075    public Response invokeDELETE(UrlBuilder url, BindingSession session) {
076        return invoke(url, "DELETE", null, null, null, session, null, null);
077    }
078
079    protected Response invoke(UrlBuilder url, String method, String contentType, Map<String, String> headers,
080            Output writer, BindingSession session, BigInteger offset, BigInteger length) {
081        Response response;
082        try {
083            response = (Response) invokeMethod.invoke(invoker, url, method, contentType, headers, writer, session,
084                    offset, length);
085        } catch (ReflectiveOperationException e) {
086            throw new RuntimeException(e);
087        }
088        lastStatus = response.getResponseCode();
089        return response;
090    }
091
092}