下面列出了怎么用org.apache.commons.io.output.ProxyOutputStream的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
MCRSessionMgr.unlock();
MCRSession requestSession = (MCRSession) requestContext.getProperty(ATTR);
if (responseContext.hasEntity()) {
responseContext.setEntityStream(new ProxyOutputStream(responseContext.getEntityStream()) {
@Override
public void close() throws IOException {
LOGGER.debug("Closing EntityStream");
try {
super.close();
} finally {
releaseSessionIfNeeded(requestSession);
LOGGER.debug("Closing EntityStream done");
}
}
});
} else {
LOGGER.debug("No Entity in response, closing MCRSession");
releaseSessionIfNeeded(requestSession);
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
LOGGER.debug("ResponseFilter start");
try {
MCRSessionMgr.unlock();
MCRSession currentSession = MCRSessionMgr.getCurrentSession();
if (responseContext.getStatus() == Response.Status.FORBIDDEN.getStatusCode() && currentSession
.getUserInformation().getUserID().equals(MCRSystemUserInformation.getGuestInstance().getUserID())) {
LOGGER.debug("Guest detected, change response from FORBIDDEN to UNAUTHORIZED.");
responseContext.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
responseContext.getHeaders().putSingle(HttpHeaders.WWW_AUTHENTICATE,
MCRRestAPIUtil.getWWWAuthenticateHeader("Basic", null, app));
}
if (responseContext.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()
&& doNotWWWAuthenticate(requestContext)) {
LOGGER.debug("Remove {} header.", HttpHeaders.WWW_AUTHENTICATE);
responseContext.getHeaders().remove(HttpHeaders.WWW_AUTHENTICATE);
}
addJWTToResponse(requestContext, responseContext);
if (responseContext.hasEntity()) {
responseContext.setEntityStream(new ProxyOutputStream(responseContext.getEntityStream()) {
@Override
public void close() throws IOException {
LOGGER.debug("Closing EntityStream");
try {
super.close();
} finally {
closeSessionIfNeeded();
LOGGER.debug("Closing EntityStream done");
}
}
});
} else {
LOGGER.debug("No Entity in response, closing MCRSession");
closeSessionIfNeeded();
}
} finally {
LOGGER.debug("ResponseFilter stop");
}
}
/**
* Returns an output stream for the requested file.
*
* After completing the write the contents must be committed using the {@link #commit(java.io.OutputStream)}
* method and the stream must be released using the {@link #release()} method.
*
* Example usage:
*
* DataStore dataStore = new DataStore(...);
* try (OutputStream os = dataStore.getOutputStream()) {
* os.write(..);
* dataStore.commit(os);
* } catch (IOException e) {
* ...
* } finally {
* dataStore.release();
* }
*
* @return
* @throws IOException
*/
public OutputStream getOutputStream() throws IOException {
acquireLock();
try {
isClosed = false;
forWrite = true;
LOG.trace("Starts write '{}'", file);
verifyAndRecover();
FileAttribute permissions = DEFAULT_PERMISSIONS;
if (Files.exists(file)) {
permissions = PosixFilePermissions.asFileAttribute(Files.getPosixFilePermissions(file));
Files.move(file, fileOld);
LOG.trace("Starting write, move '{}' to '{}'", file, fileOld);
}
Files.createFile(fileTmp, permissions);
OutputStream os = new ProxyOutputStream(new FileOutputStream(fileTmp.toFile())) {
@Override
public void close() throws IOException {
if (isClosed) {
return;
}
try {
super.close();
} finally {
isClosed = true;
stream = null;
}
LOG.trace("Finishes write '{}'", file);
}
};
stream = os;
return os;
} catch (Exception ex) {
release();
throw ex;
}
}