private HttpSession session() { return sessionFactory.getSession(request); }
@Override public void dispose(final HttpSession session) { LOGGER.trace("Disposing session {}", session); session.expire(); } }
/** * Commit a non-batch session */ public void commit() { if (!isBatchSession()) { session.commit(); } }
/** * Create a new identifier converter within the given session with the given URI template * @param session the session * @param uriBuilder the uri builder */ public HttpResourceConverter(final HttpSession session, final UriBuilder uriBuilder) { this.session = session.getFedoraSession(); this.uriBuilder = uriBuilder; this.batch = session.isBatchSession(); this.uriTemplate = new UriTemplate(uriBuilder.toTemplate()); resetTranslationChain(); }
private void checkExistingAncestor(final String path) { // check the closest existing ancestor for containment violations. String parentPath = path.substring(0, path.lastIndexOf("/")); while (!(parentPath.isEmpty() || parentPath.equals("/"))) { if (nodeService.exists(session.getFedoraSession(), parentPath)) { if (!(getResourceFromPath(parentPath) instanceof Container)) { throw new ClientErrorException("Unable to add child " + path.replace(parentPath, "") + " to resource " + parentPath + ".", CONFLICT); } break; } parentPath = parentPath.substring(0, parentPath.lastIndexOf("/")); } }
/** * Get a JCR session for the given HTTP servlet request with a * SecurityContext attached * * @param servletRequest the servlet request * @return the Session * @throws RuntimeException if the transaction could not be found */ public HttpSession getSession(final HttpServletRequest servletRequest) { final HttpSession session; final String txId = getEmbeddedId(servletRequest, Prefix.TX); try { if (txId == null) { session = createSession(servletRequest); } else { session = getSessionFromTransaction(servletRequest, txId); } } catch (final SessionMissingException e) { LOGGER.warn("Transaction missing: {}", e.getMessage()); return null; } return session; }
/** * Retrieve a JCR session from an active transaction * * @param servletRequest the servlet request * @param txId the transaction id * @return a JCR session that is associated with the transaction */ protected HttpSession getSessionFromTransaction(final HttpServletRequest servletRequest, final String txId) { final Principal userPrincipal = servletRequest.getUserPrincipal(); final String userName = userPrincipal == null ? null : userPrincipal.getName(); final FedoraSession session = batchService.getSession(txId, userName); final HttpSession batchSession = new HttpSession(session); batchSession.makeBatchSession(); LOGGER.debug("Returning a session in the batch {} for user {}", batchSession, userName); return batchSession; }
private Response finalizeTransaction(@PathParam("path") final String externalPath, final String username, final boolean commit) { final String path = toPath(translator(), externalPath); if (!path.equals("/")) { return status(BAD_REQUEST).build(); } if (!session.isBatchSession()) { LOGGER.debug("cannot finalize an empty tx id {} at path {}", session.getId(), path); return status(BAD_REQUEST).build(); } if (commit) { LOGGER.debug("commiting transaction {} at path {}", session.getId(), path); batchService.commit(session.getId(), username); } else { LOGGER.debug("rolling back transaction {} at path {}", session.getId(), path); batchService.abort(session.getId(), username); } return noContent().build(); } }
/** * Delete a tombstone resource (freeing the original resource to be reused) * @return the free resource */ @DELETE public Response delete() { LOGGER.info("Delete tombstone: {}", resource()); resource().delete(); session.commit(); return noContent().build(); }
/** * Extract the id embedded at the beginning of a request path * * @param servletRequest the servlet request * @param prefix the prefix for the id * @return the found id or null */ protected String getEmbeddedId( final HttpServletRequest servletRequest, final Prefix prefix) { String requestPath = servletRequest.getPathInfo(); // http://stackoverflow.com/questions/18963562/grizzlys-request-getpathinfo-returns-always-null if (requestPath == null && servletRequest.getContextPath().isEmpty()) { requestPath = servletRequest.getRequestURI(); } String id = null; if (requestPath != null) { final String pathPrefix = prefix.getPrefix(); final String[] part = requestPath.split("/"); if (part.length > 1 && part[1].startsWith(pathPrefix)) { id = part[1].substring(pathPrefix.length()); } } return id; }
/** * Create a JCR session for the given HTTP servlet request with a * SecurityContext attached. * * @param servletRequest the servlet request * @return a newly created JCR session */ protected HttpSession createSession(final HttpServletRequest servletRequest) { LOGGER.debug("Returning an authenticated session in the default workspace"); return new HttpSession(repo.login(credentialsService.getCredentials(servletRequest))); }
final Session internalSession = sessions.getInternalSession(); try {
/** * Expire a non-batch session */ public void expire() { if (!isBatchSession()) { session.expire(); } }
/** * Set the baseURL for JMS events. * @param uriInfo the uri info * @param headers HTTP headers **/ protected void setUpJMSInfo(final UriInfo uriInfo, final HttpHeaders headers) { try { String baseURL = getBaseUrlProperty(uriInfo); if (baseURL.length() == 0) { baseURL = uriInfo.getBaseUri().toString(); } LOGGER.debug("setting baseURL = " + baseURL); session.getFedoraSession().addSessionData(BASE_URL, baseURL); if (!StringUtils.isBlank(headers.getHeaderString("user-agent"))) { session.getFedoraSession().addSessionData(USER_AGENT, headers.getHeaderString("user-agent")); } } catch (final Exception ex) { LOGGER.warn("Error setting baseURL", ex.getMessage()); } }
@Override public HttpSession provide() { final HttpSession session = sessionFactory.getSession(request); LOGGER.trace("Providing new session {}", session); return session; }
/** * Deletes an object. * * @return response */ @DELETE public Response deleteObject() { hasRestrictedPath(externalPath); if (resource() instanceof Container) { final String depth = headers.getHeaderString("Depth"); LOGGER.debug("Depth header value is: {}", depth); if (depth != null && !depth.equalsIgnoreCase("infinity")) { throw new ClientErrorException("Depth header, if present, must be set to 'infinity' for containers", SC_BAD_REQUEST); } } evaluateRequestPreconditions(request, servletResponse, resource(), session); LOGGER.info("Delete resource '{}'", externalPath); final AcquiredLock lock = lockManager.lockForDelete(resource().getPath()); try { resource().delete(); session.commit(); return noContent().build(); } finally { lock.release(); } }
@Override protected void addResourceHttpHeaders(final FedoraResource resource) { super.addResourceHttpHeaders(resource); if (session.isBatchSession()) { final String canonical = translator().reverse() .convert(resource) .toString() .replaceFirst("/tx:[^/]+", ""); servletResponse.addHeader(LINK, "<" + canonical + ">;rel=\"canonical\""); } addExternalContentHeaders(resource); }
private FedoraBinary createBinaryMementoFromRequest(final FedoraBinary binaryResource, final Instant mementoInstant, final InputStream requestBodyStream, final ExternalContentHandler extContent, final String digest) throws InvalidChecksumException, UnsupportedAlgorithmException { final Collection<String> checksums = parseDigestHeader(digest); final Collection<URI> checksumURIs = checksums == null ? new HashSet<>() : checksums.stream().map( checksum -> checksumURI(checksum)).collect(Collectors.toSet()); // Create internal binary either from supplied body or copy external uri if (extContent == null || extContent.isCopy()) { InputStream contentStream = requestBodyStream; if (extContent != null) { contentStream = extContent.fetchExternalContent(); } return versionService.createBinaryVersion(session.getFedoraSession(), binaryResource, mementoInstant, contentStream, checksumURIs, storagePolicyDecisionPoint); } else { return versionService.createExternalBinaryVersion(session.getFedoraSession(), binaryResource, mementoInstant, checksumURIs, extContent.getHandling(), extContent.getURL()); } }
final HttpSession session) { if (session.isBatchSession()) {
private FedoraResource createFedoraResource(final String path, final String interactionModel, final MediaType contentType, final boolean contentPresent, final boolean contentExternal) { final MediaType simpleContentType = contentPresent ? getSimpleContentType(contentType) : null; final FedoraResource result; if ("ldp:NonRDFSource".equals(interactionModel) || contentExternal || (contentPresent && interactionModel == null && !isRDF(simpleContentType))) { result = binaryService.findOrCreate(session.getFedoraSession(), path); } else { result = containerService.findOrCreate(session.getFedoraSession(), path); } final String resInteractionModel = getInteractionModel(result); if (StringUtils.isNoneBlank(interactionModel) && StringUtils.isNoneBlank(resInteractionModel) && !resInteractionModel.equals(interactionModel)) { throw new InteractionModelViolationException("Changing the interaction model " + resInteractionModel + " to " + interactionModel + " is not allowed!"); } return result; }