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 protected final DefaultHttpInvoker invoker; 040 041 protected Method invokeMethod; 042 043 public StatusLoggingDefaultHttpInvoker() { 044 // we delegate instead of subclassing because the method we're 045 // interested in overriding (invoke) is private... 046 invoker = new DefaultHttpInvoker(); 047 for (Method m : invoker.getClass().getDeclaredMethods()) { 048 if (m.getName().equals("invoke")) { 049 invokeMethod = m; 050 invokeMethod.setAccessible(true); 051 break; 052 } 053 } 054 } 055 056 public Response invokeGET(UrlBuilder url, BindingSession session) { 057 return invoke(url, "GET", null, null, null, session, null, null); 058 } 059 060 public Response invokeGET(UrlBuilder url, BindingSession session, BigInteger offset, BigInteger length) { 061 return invoke(url, "GET", null, null, null, session, offset, length); 062 } 063 064 public Response invokePOST(UrlBuilder url, String contentType, Output writer, BindingSession session) { 065 return invoke(url, "POST", contentType, null, writer, session, null, null); 066 } 067 068 public Response invokePUT(UrlBuilder url, String contentType, Map<String, String> headers, Output writer, 069 BindingSession session) { 070 return invoke(url, "PUT", contentType, headers, writer, session, null, null); 071 } 072 073 public Response invokeDELETE(UrlBuilder url, BindingSession session) { 074 return invoke(url, "DELETE", null, null, null, session, null, null); 075 } 076 077 protected Response invoke(UrlBuilder url, String method, String contentType, Map<String, String> headers, 078 Output writer, BindingSession session, BigInteger offset, BigInteger length) { 079 Response response; 080 try { 081 response = (Response) invokeMethod.invoke(invoker, url, method, contentType, headers, writer, session, 082 offset, length); 083 } catch (ReflectiveOperationException e) { 084 throw new RuntimeException(e); 085 } 086 return response; 087 } 088 089}