|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | + |
| 41 | +package org.glassfish.jersey.apache.connector; |
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.io.InputStream; |
| 45 | +import java.io.OutputStream; |
| 46 | +import java.util.logging.Logger; |
| 47 | + |
| 48 | +import javax.ws.rs.POST; |
| 49 | +import javax.ws.rs.Path; |
| 50 | +import javax.ws.rs.ServerErrorException; |
| 51 | +import javax.ws.rs.client.Entity; |
| 52 | +import javax.ws.rs.client.WebTarget; |
| 53 | +import javax.ws.rs.core.Application; |
| 54 | +import javax.ws.rs.core.MediaType; |
| 55 | +import javax.ws.rs.core.Response; |
| 56 | +import javax.ws.rs.core.Response.Status; |
| 57 | +import javax.ws.rs.core.StreamingOutput; |
| 58 | + |
| 59 | +import org.glassfish.jersey.client.ClientConfig; |
| 60 | +import org.glassfish.jersey.logging.LoggingFeature; |
| 61 | +import org.glassfish.jersey.server.ResourceConfig; |
| 62 | +import org.glassfish.jersey.test.JerseyTest; |
| 63 | + |
| 64 | +import org.junit.Assert; |
| 65 | +import org.junit.Test; |
| 66 | + |
| 67 | +/** |
| 68 | + * The LargeDataTest reproduces a problem when bytes of large data sent are incorrectly sent. |
| 69 | + * As a result, the request body is different than what was sent by the client. |
| 70 | + * <p> |
| 71 | + * In order to be able to inspect the request body, the generated data is a sequence of numbers |
| 72 | + * delimited with new lines. Such as |
| 73 | + * <pre><code> |
| 74 | + * 1 |
| 75 | + * 2 |
| 76 | + * 3 |
| 77 | + * |
| 78 | + * ... |
| 79 | + * |
| 80 | + * 57234 |
| 81 | + * 57235 |
| 82 | + * 57236 |
| 83 | + * |
| 84 | + * ... |
| 85 | + * </code></pre> |
| 86 | + * It is also possible to send the data to netcat: {@code nc -l 8080} and verify the problem is |
| 87 | + * on the client side. |
| 88 | + * |
| 89 | + * @author Stepan Vavra (stepan.vavra at oracle.com) |
| 90 | + * @author Marek Potociar (marek.potociar at oracle.com) |
| 91 | + */ |
| 92 | +public class LargeDataTest extends JerseyTest { |
| 93 | + |
| 94 | + private static final Logger LOGGER = Logger.getLogger(LargeDataTest.class.getName()); |
| 95 | + private static final int LONG_DATA_SIZE = 1_000_000; // for large set around 5GB, try e.g.: 536_870_912; |
| 96 | + private static volatile Throwable exception; |
| 97 | + |
| 98 | + private static StreamingOutput longData(long sequence) { |
| 99 | + return out -> { |
| 100 | + long offset = 0; |
| 101 | + while (offset < sequence) { |
| 102 | + out.write(Long.toString(offset).getBytes()); |
| 103 | + out.write('\n'); |
| 104 | + offset++; |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + protected Application configure() { |
| 111 | + ResourceConfig config = new ResourceConfig(HttpMethodResource.class); |
| 112 | + config.register(new LoggingFeature(LOGGER, LoggingFeature.Verbosity.HEADERS_ONLY)); |
| 113 | + return config; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + protected void configureClient(ClientConfig config) { |
| 118 | + config.connectorProvider(new ApacheConnectorProvider()); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void postWithLargeData() throws Throwable { |
| 123 | + WebTarget webTarget = target("test"); |
| 124 | + |
| 125 | + Response response = webTarget.request().post(Entity.entity(longData(LONG_DATA_SIZE), MediaType.TEXT_PLAIN_TYPE)); |
| 126 | + |
| 127 | + try { |
| 128 | + if (exception != null) { |
| 129 | + |
| 130 | + // the reason to throw the exception is that IntelliJ gives you an option to compare the expected with the actual |
| 131 | + throw exception; |
| 132 | + } |
| 133 | + |
| 134 | + Assert.assertEquals("Unexpected error: " + response.getStatus(), |
| 135 | + Status.Family.SUCCESSFUL, |
| 136 | + response.getStatusInfo().getFamily()); |
| 137 | + } finally { |
| 138 | + response.close(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Path("/test") |
| 143 | + public static class HttpMethodResource { |
| 144 | + |
| 145 | + @POST |
| 146 | + public Response post(InputStream content) { |
| 147 | + try { |
| 148 | + |
| 149 | + longData(LONG_DATA_SIZE).write(new OutputStream() { |
| 150 | + |
| 151 | + private long position = 0; |
| 152 | +// private long mbRead = 0; |
| 153 | + |
| 154 | + @Override |
| 155 | + public void write(final int generated) throws IOException { |
| 156 | + int received = content.read(); |
| 157 | + |
| 158 | + if (received != generated) { |
| 159 | + throw new IOException("Bytes don't match at position " + position |
| 160 | + + ": received=" + received |
| 161 | + + ", generated=" + generated); |
| 162 | + } |
| 163 | + |
| 164 | + position++; |
| 165 | +// if (position % (1024 * 1024) == 0) { |
| 166 | +// mbRead++; |
| 167 | +// System.out.println("MB read: " + mbRead); |
| 168 | +// } |
| 169 | + } |
| 170 | + }); |
| 171 | + } catch (IOException e) { |
| 172 | + exception = e; |
| 173 | + throw new ServerErrorException(e.getMessage(), 500, e); |
| 174 | + } |
| 175 | + |
| 176 | + return Response.ok().build(); |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | +} |
0 commit comments