Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed servlet 3.1 feaures #453

Merged
merged 1 commit into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions logbook-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<artifactId>logbook-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.zalando.logbook.servlet;

import lombok.AllArgsConstructor;
import org.zalando.logbook.Headers;
import org.zalando.logbook.HttpResponse;
import org.zalando.logbook.Origin;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -117,7 +119,7 @@ private static class Tee {
private PrintWriter writer;
private byte[] bytes;

private Tee(final OutputStream original) {
private Tee(final ServletOutputStream original) {
this.branch = new ByteArrayOutputStream();
this.output = new TeeServletOutputStream(original, branch);
}
Expand All @@ -141,16 +143,12 @@ byte[] getBytes() {
}
}

@AllArgsConstructor
private static class TeeServletOutputStream extends ServletOutputStream {

private final OutputStream original;
private final ServletOutputStream original;
private final OutputStream branch;

private TeeServletOutputStream(final OutputStream original, final OutputStream branch) {
this.original = original;
this.branch = branch;
}

@Override
public void write(final int b) throws IOException {
original.write(b);
Expand All @@ -175,5 +173,15 @@ public void close() throws IOException {
branch.close();
}

@Override
public boolean isReady() {
return original.isReady();
}

@Override
public void setWriteListener(final WriteListener listener) {
original.setWriteListener(listener);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package org.zalando.logbook.servlet;

import lombok.AllArgsConstructor;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@AllArgsConstructor
final class ServletInputStreamAdapter extends ServletInputStream {

private final InputStream stream;

public ServletInputStreamAdapter(final InputStream stream) {
this.stream = stream;
}
private final ByteArrayInputStream stream;

@Override
public int read() throws IOException {
public int read() {
return stream.read();
}

Expand All @@ -23,8 +23,23 @@ public int read(final byte[] b) throws IOException {
}

@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
public int read(final byte[] b, final int off, final int len) {
return stream.read(b, off, len);
}

@Override
public boolean isFinished() {
return stream.available() == 0;
}

@Override
public boolean isReady() {
return true;
}

@Override
public void setReadListener(final ReadListener readListener) {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import org.junit.jupiter.api.Test;

import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.mock;
Expand All @@ -28,6 +30,16 @@ class LocalResponseTest {
void setUp() throws IOException {
mock = mock(HttpServletResponse.class);
when(mock.getOutputStream()).thenReturn(new ServletOutputStream() {
@Override
public boolean isReady() {
return false;
}

@Override
public void setWriteListener(final WriteListener listener) {
// nothing to do here
}

@Override
public void write(final int b) {
// serves as a null or no-op output stream
Expand Down Expand Up @@ -119,4 +131,17 @@ void shouldReturnNullContentTypeWhenNoContentTypeHasBeenSpecified() {

assertThat(unit.getContentType(), is(nullValue()));
}

@Test
void shouldBeReady() throws IOException {
unit.withBody();
assertFalse(unit.getOutputStream().isReady());
}

@Test
void shouldNotSupportWriteListener() throws IOException {
unit.withBody();
unit.getOutputStream().setWriteListener(mock(WriteListener.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.zalando.logbook.servlet;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ServletInputStreamAdapterTest {

private final ServletInputStream unit = new ServletInputStreamAdapter(new ByteArrayInputStream(
"Hello, world".getBytes(UTF_8)));

@Test
void shouldBeReady() {
assertTrue(unit.isReady());
}

@Test
void shouldBeFinishedWhenDone() throws IOException {
assertFalse(unit.isFinished());
ByteStreams.copy(unit, NullOutputStream.NULL);
assertTrue(unit.isFinished());
}

@Test
void shouldNotSupportReadListener() {
assertThrows(UnsupportedOperationException.class, () ->
unit.setReadListener(Mockito.mock(ReadListener.class)));
}

}
4 changes: 2 additions & 2 deletions logbook-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<artifactId>logbook-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@
</dependency>
<!-- javax -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.1.1</version>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- spring -->
Expand Down