001/* 002 * (C) Copyright 2006-2016 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 * bstefanescu 018 */ 019package org.nuxeo.runtime.test.runner; 020 021import java.lang.reflect.Field; 022 023import org.nuxeo.runtime.server.ServerComponent; 024 025import sun.net.www.http.HttpClient; 026 027/** 028 * Runs an embedded servlet container. 029 * <p> 030 * Note that at initialization the feature disables the {@code retryPostProp} property of 031 * {@link sun.net.www.http.HttpClient}, the underlying HTTP client used by {@link com.sun.jersey.api.client.Client}. 032 * <p> 033 * This is to prevent the JDK's default behavior kept for backward compatibility: an unsuccessful HTTP POST request is 034 * automatically resent to the server, unsuccessful in this case meaning the server did not send a valid HTTP response 035 * or an {@code IOException} occurred. Yet in the tests using the Jersey client to make calls to Nuxeo we don't want 036 * this as it can hide errors occurring in the HTTP communication that should prevent an appropriate response from being 037 * sent by the server. 038 */ 039@Deploy("org.nuxeo.runtime.server") 040@Features(RuntimeFeature.class) 041public class ServletContainerFeature extends SimpleFeature { 042 043 @Override 044 public void initialize(FeaturesRunner runner) throws Exception { 045 disableSunHttpClientRetryPostProp(); 046 047 ServletContainer conf = runner.getConfig(ServletContainer.class); 048 if (conf == null) { 049 conf = Defaults.of(ServletContainer.class); 050 } 051 configureServletContainer(conf); 052 } 053 054 protected void configureServletContainer(ServletContainer conf) { 055 int p = conf.port(); 056 if (p > 0) { 057 System.setProperty(ServerComponent.PORT_SYSTEM_PROP, String.valueOf(p)); 058 } 059 } 060 061 /** 062 * Prevents the JDK's default behavior of resending an unsuccessful HTTP POST request automatically to the server by 063 * disabling the the {@code retryPostProp} property of {@link sun.net.www.http.HttpClient}. 064 * <p> 065 * This can also be achieved by setting the {@code sun.net.http.retryPost} system property to {@code false}. 066 * 067 * @since 9.3 068 */ 069 public static void disableSunHttpClientRetryPostProp() 070 throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { 071 Field field = HttpClient.class.getDeclaredField("retryPostProp"); 072 field.setAccessible(true); 073 field.setBoolean(null, false); 074 } 075 076}