|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.cxf.systest.jaxws; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.io.OutputStream; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.concurrent.ThreadLocalRandom; |
| 28 | +import java.util.logging.Logger; |
| 29 | + |
| 30 | +import javax.activation.DataHandler; |
| 31 | +import javax.activation.DataSource; |
| 32 | +import javax.xml.ws.Binding; |
| 33 | +import javax.xml.ws.BindingProvider; |
| 34 | +import javax.xml.ws.Endpoint; |
| 35 | +import javax.xml.ws.soap.SOAPBinding; |
| 36 | +import javax.xml.ws.soap.SOAPFaultException; |
| 37 | + |
| 38 | +import org.apache.cxf.Download; |
| 39 | +import org.apache.cxf.DownloadFault_Exception; |
| 40 | +import org.apache.cxf.DownloadNextResponseType; |
| 41 | +import org.apache.cxf.common.logging.LogUtils; |
| 42 | +import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; |
| 43 | +import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase; |
| 44 | +import org.apache.cxf.testutil.common.AbstractBusTestServerBase; |
| 45 | + |
| 46 | +import org.junit.BeforeClass; |
| 47 | +import org.junit.Test; |
| 48 | + |
| 49 | +import static org.hamcrest.CoreMatchers.containsString; |
| 50 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 51 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 52 | +import static org.junit.Assert.assertThrows; |
| 53 | +import static org.junit.Assert.assertTrue; |
| 54 | + |
| 55 | +public class AttachmentChunkingTest extends AbstractBusClientServerTestBase { |
| 56 | + private static final String PORT = allocatePort(DownloadServer.class); |
| 57 | + private static final Logger LOG = LogUtils.getLogger(AttachmentChunkingTest.class); |
| 58 | + |
| 59 | + private static final class DownloadImpl implements Download { |
| 60 | + @Override |
| 61 | + public DownloadNextResponseType downloadNext(Boolean simulate) { |
| 62 | + final DownloadNextResponseType responseType = new DownloadNextResponseType(); |
| 63 | + responseType.setDataContent(new DataHandler(new DataSource() { |
| 64 | + @Override |
| 65 | + public InputStream getInputStream() { |
| 66 | + if (simulate) { |
| 67 | + return simulate(); |
| 68 | + } else { |
| 69 | + return generate(100000); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public OutputStream getOutputStream() { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String getContentType() { |
| 80 | + return ""; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String getName() { |
| 85 | + return ""; |
| 86 | + } |
| 87 | + })); |
| 88 | + |
| 89 | + return responseType; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static class DownloadServer extends AbstractBusTestServerBase { |
| 94 | + protected void run() { |
| 95 | + Object implementor = new DownloadImpl(); |
| 96 | + String address = "http://localhost:" + PORT + "/SoapContext/SoapPort"; |
| 97 | + Endpoint.publish(address, implementor); |
| 98 | + } |
| 99 | + |
| 100 | + public static void main(String[] args) { |
| 101 | + try { |
| 102 | + DownloadServer s = new DownloadServer(); |
| 103 | + s.start(); |
| 104 | + } catch (Exception ex) { |
| 105 | + ex.printStackTrace(); |
| 106 | + System.exit(-1); |
| 107 | + } finally { |
| 108 | + LOG.info("done!"); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @BeforeClass |
| 114 | + public static void startServers() throws Exception { |
| 115 | + assertTrue("server did not launch correctly", launchServer(DownloadServer.class, true)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testChunkingPartialFailure() { |
| 120 | + final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); |
| 121 | + factory.setServiceClass(Download.class); |
| 122 | + |
| 123 | + final Download client = (Download) factory.create(); |
| 124 | + final BindingProvider bindingProvider = (BindingProvider) client; |
| 125 | + final Binding binding = bindingProvider.getBinding(); |
| 126 | + |
| 127 | + final String address = String.format("http://localhost:%s/SoapContext/SoapPort/DownloadPort", PORT); |
| 128 | + bindingProvider.getRequestContext().put("jakarta.xml.ws.service.endpoint.address", address); |
| 129 | + ((SOAPBinding) binding).setMTOMEnabled(true); |
| 130 | + |
| 131 | + // See please https://issues.apache.org/jira/browse/CXF-9057 |
| 132 | + SOAPFaultException ex = assertThrows(SOAPFaultException.class, () -> client.downloadNext(true)); |
| 133 | + assertThat(ex.getMessage(), containsString("simulated error during stream processing")); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testChunking() throws IOException, DownloadFault_Exception { |
| 138 | + final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); |
| 139 | + factory.setServiceClass(Download.class); |
| 140 | + |
| 141 | + final Download client = (Download) factory.create(); |
| 142 | + final BindingProvider bindingProvider = (BindingProvider) client; |
| 143 | + final Binding binding = bindingProvider.getBinding(); |
| 144 | + |
| 145 | + final String address = String.format("http://localhost:%s/SoapContext/SoapPort/DownloadPort", PORT); |
| 146 | + bindingProvider.getRequestContext().put("jakarta.xml.ws.service.endpoint.address", address); |
| 147 | + ((SOAPBinding) binding).setMTOMEnabled(true); |
| 148 | + |
| 149 | + final DownloadNextResponseType response = client.downloadNext(false); |
| 150 | + assertThat(response.getDataContent().getInputStream().readAllBytes().length, equalTo(100000)); |
| 151 | + } |
| 152 | + |
| 153 | + private static InputStream generate(int size) { |
| 154 | + final byte[] buf = new byte[size]; |
| 155 | + Arrays.fill(buf, (byte) 'x'); |
| 156 | + return new ByteArrayInputStream(buf); |
| 157 | + } |
| 158 | + |
| 159 | + private static InputStream simulate() { |
| 160 | + return new InputStream() { |
| 161 | + @Override |
| 162 | + public int read() { |
| 163 | + return (byte) 'x'; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public int read(byte[] b, int off, int len) { |
| 168 | + if (ThreadLocalRandom.current().nextBoolean()) { |
| 169 | + throw new IllegalArgumentException("simulated error during stream processing"); |
| 170 | + } |
| 171 | + |
| 172 | + for (int i = off; i < off + len; i++) { |
| 173 | + b[i] = (byte) 'x'; |
| 174 | + } |
| 175 | + |
| 176 | + return len; |
| 177 | + } |
| 178 | + }; |
| 179 | + } |
| 180 | +} |
0 commit comments