org.apache.commons.io.output.ProxyOutputStream Java Examples

The following examples show how to use org.apache.commons.io.output.ProxyOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MCRSessionHookFilter.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@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);
    }
}
 
Example #2
Source File: MCRSessionFilter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@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");
    }
}
 
Example #3
Source File: DataStore.java    From datacollector with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}