forked from eclipse-ee4j/jersey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EntityInputStream#isEmpty refactoring
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
- Loading branch information
Showing
7 changed files
with
238 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...et-core/src/main/java/org/glassfish/jersey/servlet/internal/ServletEntityInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.servlet.internal; | ||
|
||
import jakarta.servlet.ReadListener; | ||
import jakarta.servlet.ServletInputStream; | ||
import org.glassfish.jersey.message.internal.EntityInputStream; | ||
import org.glassfish.jersey.message.internal.EntityInputStreamListener; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
public abstract class ServletEntityInputStream extends ServletInputStream { | ||
|
||
protected EntityInputStream wrappedStream; | ||
|
||
|
||
protected abstract ServletInputStream getServletInputStream(); | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return getServletInputStream().isFinished(); | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return getServletInputStream().isReady(); | ||
} | ||
|
||
@Override | ||
public void setReadListener(ReadListener readListener) { | ||
getServletInputStream().setReadListener(readListener); | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
return getServletInputStream().read(); | ||
} | ||
|
||
public EntityInputStream getWrappedStream() { | ||
if (wrappedStream == null) { | ||
wrappedStream = new EntityInputStream(getServletInputStream()); | ||
wrappedStream.setListener(new EntityInputStreamListener() { | ||
@Override | ||
public boolean isEmpty() { | ||
try { | ||
return getServletInputStream().available() == 0 | ||
|| getServletInputStream().isFinished(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return getServletInputStream().isReady(); | ||
} | ||
}); | ||
} | ||
|
||
return wrappedStream; | ||
} | ||
|
||
|
||
} |
53 changes: 53 additions & 0 deletions
53
...core/src/main/java/org/glassfish/jersey/servlet/internal/ServletRequestEntityWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.servlet.internal; | ||
|
||
import jakarta.servlet.ServletInputStream; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import org.glassfish.jersey.message.internal.EntityInputStream; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
|
||
public class ServletRequestEntityWrapper extends HttpServletRequestWrapper { | ||
|
||
private final ServletEntityInputStream servletInputStream = new ServletEntityInputStream() { | ||
@Override | ||
protected ServletInputStream getServletInputStream() { | ||
try { | ||
return getRequest().getInputStream(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
}; | ||
|
||
public ServletRequestEntityWrapper(HttpServletRequest request) { | ||
super(request); | ||
} | ||
|
||
@Override | ||
public ServletInputStream getInputStream() throws IOException { | ||
return servletInputStream; | ||
} | ||
|
||
public EntityInputStream getWrappedInputStream() { | ||
return servletInputStream.getWrappedStream(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...common/src/main/java/org/glassfish/jersey/message/internal/EntityInputStreamListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.message.internal; | ||
|
||
import java.util.EventListener; | ||
|
||
/** | ||
* Provides possibility to externally check whether an input stream for an entity is empty or not. | ||
* | ||
* Is being used in the {@link EntityInputStream#isEmpty()} check | ||
*/ | ||
public interface EntityInputStreamListener extends EventListener { | ||
|
||
/** | ||
* Provides information if the underlying stream is empty | ||
* | ||
* @return true if the underlying stream is empty | ||
*/ | ||
boolean isEmpty(); | ||
|
||
/** | ||
* Can be used to provide readiness information. | ||
* <p> | ||
* If the stream is not ready the calling check in the {@link EntityInputStream#isEmpty()} method will validate | ||
* the underlying stream as not empty. | ||
* </p> | ||
* @return true if the underlying stream is ready. | ||
*/ | ||
boolean isReady(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters