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 * Florent Guillaume 016 */ 017package org.nuxeo.ecm.core.opencmis.tests; 018 019import java.lang.reflect.Method; 020import java.math.BigInteger; 021import java.util.Map; 022 023import org.apache.chemistry.opencmis.client.bindings.spi.BindingSession; 024import org.apache.chemistry.opencmis.client.bindings.spi.http.DefaultHttpInvoker; 025import org.apache.chemistry.opencmis.client.bindings.spi.http.HttpInvoker; 026import org.apache.chemistry.opencmis.client.bindings.spi.http.Output; 027import org.apache.chemistry.opencmis.client.bindings.spi.http.Response; 028import org.apache.chemistry.opencmis.commons.impl.UrlBuilder; 029 030/** 031 * HTTP Invoker that notes the last status returned. 032 * 033 * @since 7.1 034 */ 035public class StatusLoggingDefaultHttpInvoker implements HttpInvoker { 036 037 public static int lastStatus; 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 lastStatus = response.getResponseCode(); 087 return response; 088 } 089 090}