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