|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2015 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 | + |
| 46 | +import javax.ws.rs.GET; |
| 47 | +import javax.ws.rs.Path; |
| 48 | +import javax.ws.rs.Produces; |
| 49 | +import javax.ws.rs.client.WebTarget; |
| 50 | +import javax.ws.rs.core.Application; |
| 51 | +import javax.ws.rs.core.MediaType; |
| 52 | + |
| 53 | +import javax.inject.Singleton; |
| 54 | + |
| 55 | +import org.glassfish.jersey.client.ClientConfig; |
| 56 | +import org.glassfish.jersey.server.ChunkedOutput; |
| 57 | +import org.glassfish.jersey.server.ResourceConfig; |
| 58 | +import org.glassfish.jersey.test.JerseyTest; |
| 59 | + |
| 60 | +import org.junit.Test; |
| 61 | +import static org.junit.Assert.assertEquals; |
| 62 | + |
| 63 | +/** |
| 64 | + * @author Petr Janouch (petr.janouch at oracle.com) |
| 65 | + */ |
| 66 | +public class StreamingTest extends JerseyTest { |
| 67 | + |
| 68 | + /** |
| 69 | + * Test that a data stream can be terminated from the client side. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + public void clientCloseTest() throws IOException { |
| 73 | + // start streaming |
| 74 | + InputStream inputStream = target().path("/streamingEndpoint").request().get(InputStream.class); |
| 75 | + |
| 76 | + WebTarget sendTarget = target().path("/streamingEndpoint/send"); |
| 77 | + // trigger sending 'A' to the stream; OK is sent if everything on the server was OK |
| 78 | + assertEquals("OK", sendTarget.request().get().readEntity(String.class)); |
| 79 | + // check 'A' has been sent |
| 80 | + assertEquals('A', inputStream.read()); |
| 81 | + // closing the stream should tear down the connection |
| 82 | + inputStream.close(); |
| 83 | + // trigger sending another 'A' to the stream; it should fail |
| 84 | + // (indicating that the streaming has been terminated on the server) |
| 85 | + assertEquals("NOK", sendTarget.request().get().readEntity(String.class)); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected void configureClient(ClientConfig config) { |
| 90 | + config.connectorProvider(new ApacheConnectorProvider()); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected Application configure() { |
| 95 | + return new ResourceConfig(StreamingEndpoint.class); |
| 96 | + } |
| 97 | + |
| 98 | + @Singleton |
| 99 | + @Path("streamingEndpoint") |
| 100 | + public static class StreamingEndpoint { |
| 101 | + |
| 102 | + private final ChunkedOutput<String> output = new ChunkedOutput<>(String.class); |
| 103 | + |
| 104 | + @GET |
| 105 | + @Path("send") |
| 106 | + public String sendEvent() { |
| 107 | + try { |
| 108 | + output.write("A"); |
| 109 | + } catch (IOException e) { |
| 110 | + return "NOK"; |
| 111 | + } |
| 112 | + |
| 113 | + return "OK"; |
| 114 | + } |
| 115 | + |
| 116 | + @GET |
| 117 | + @Produces(MediaType.TEXT_PLAIN) |
| 118 | + public ChunkedOutput<String> get() { |
| 119 | + return output; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments