use of org.exist.http.Descriptor in project exist by eXist-db.
the class RpcServlet method doPost.
@Override
public void doPost(HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException {
try {
// Request logger
final Descriptor descriptor = Descriptor.getDescriptorSingleton();
if (descriptor.allowRequestLogging() && !descriptor.requestsFiltered()) {
// Wrap HttpServletRequest, because both request Logger and xmlrpc
// need the request InputStream, which is consumed when read.
final String cacheClass = (String) BrokerPool.getInstance().getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY);
request = new HttpServletRequestWrapper(() -> cacheClass, request, /*formEncoding*/
charset != null ? charset.displayName() : ISO_8859_1.displayName());
descriptor.doLogRequestInReplayLog(request);
}
try {
if (charset != null) {
response.setCharacterEncoding(charset.displayName());
}
super.doPost(request, response);
} catch (final Throwable e) {
LOG.error("Problem during XmlRpc execution", e);
final String exceptionMessage;
if (e instanceof XmlRpcException) {
final Throwable linkedException = ((XmlRpcException) e).linkedException;
LOG.error(linkedException.getMessage(), linkedException);
exceptionMessage = "An error occurred: " + e.getMessage() + ": " + linkedException.getMessage();
} else {
exceptionMessage = "An unknown error occurred: " + e.getMessage();
}
throw new ServletException(exceptionMessage, e);
}
} catch (final EXistException e) {
throw new ServletException(e);
} finally {
if (request != null && request instanceof HttpServletRequestWrapper) {
((HttpServletRequestWrapper) request).close();
}
}
}
use of org.exist.http.Descriptor in project exist by eXist-db.
the class EXistServlet method doPost.
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse response) throws ServletException, IOException {
HttpServletRequest request = null;
try {
// For POST request, If we are logging the requests we must wrap
// HttpServletRequest in HttpServletRequestWrapper
// otherwise we cannot access the POST parameters from the content body
// of the request!!! - deliriumsky
final Descriptor descriptor = Descriptor.getDescriptorSingleton();
if (descriptor != null) {
if (descriptor.allowRequestLogging()) {
request = new HttpServletRequestWrapper(() -> (String) getPool().getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), req, getFormEncoding());
} else {
request = req;
}
} else {
request = req;
}
// first, adjust the path
String path = request.getPathInfo();
if (path == null) {
path = "";
} else {
path = adjustPath(request);
}
// second, perform descriptor actions
if (descriptor != null && !descriptor.requestsFiltered()) {
// logs the request if specified in the descriptor
descriptor.doLogRequestInReplayLog(request);
// map's the path if a mapping is specified in the descriptor
path = descriptor.mapPath(path);
}
// third, authenticate the user
final Subject user = authenticate(request, response);
if (user == null) {
// You now get a HTTP Authentication challenge if there is no user
return;
}
// fourth, process the request
try (final DBBroker broker = getPool().get(Optional.of(user));
final Txn transaction = getPool().getTransactionManager().beginTransaction()) {
try {
srvREST.doPost(broker, transaction, request, response, path);
transaction.commit();
} catch (final Throwable t) {
transaction.abort();
throw t;
}
} catch (final PermissionDeniedException e) {
// Else return a FORBIDDEN Error
if (user.equals(getDefaultUser())) {
getAuthenticator().sendChallenge(request, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
}
} catch (final EXistException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} catch (final BadRequestException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
} catch (final NotFoundException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
} catch (final Throwable e) {
getLog().error(e);
throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
}
} finally {
if (request instanceof HttpServletRequestWrapper) {
((HttpServletRequestWrapper) request).close();
}
}
}
use of org.exist.http.Descriptor in project exist by eXist-db.
the class EXistServlet method doGet.
@Override
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
// first, adjust the path
String path = adjustPath(request);
// second, perform descriptor actions
final Descriptor descriptor = Descriptor.getDescriptorSingleton();
if (descriptor != null && !descriptor.requestsFiltered()) {
// logs the request if specified in the descriptor
descriptor.doLogRequestInReplayLog(request);
// map's the path if a mapping is specified in the descriptor
path = descriptor.mapPath(path);
}
// third, authenticate the user
final Subject user = authenticate(request, response);
if (user == null) {
// You now get a HTTP Authentication challenge if there is no user
return;
}
// fourth, process the request
try (final DBBroker broker = getPool().get(Optional.of(user));
final Txn transaction = getPool().getTransactionManager().beginTransaction()) {
try {
srvREST.doGet(broker, transaction, request, response, path);
transaction.commit();
} catch (final Throwable t) {
transaction.abort();
throw t;
}
} catch (final BadRequestException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage());
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
} catch (final PermissionDeniedException e) {
// Else return a FORBIDDEN Error
if (user.equals(getDefaultUser())) {
getAuthenticator().sendChallenge(request, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
}
} catch (final NotFoundException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage());
}
response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
} catch (final EXistException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} catch (final EOFException ee) {
getLog().error("GET Connection has been interrupted", ee);
throw new ServletException("GET Connection has been interrupted", ee);
} catch (final Throwable e) {
getLog().error(e.getMessage(), e);
throw new ServletException("An error occurred: " + e.getMessage(), e);
}
}
use of org.exist.http.Descriptor in project exist by eXist-db.
the class EXistServlet method doDelete.
@Override
protected void doDelete(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
// first, adjust the path
String path = adjustPath(request);
// second, perform descriptor actions
final Descriptor descriptor = Descriptor.getDescriptorSingleton();
if (descriptor != null) {
// map's the path if a mapping is specified in the descriptor
path = descriptor.mapPath(path);
}
// third, authenticate the user
final Subject user = authenticate(request, response);
if (user == null) {
// You now get a HTTP Authentication challenge if there is no user
return;
}
// fourth, process the request
try (final DBBroker broker = getPool().get(Optional.of(user));
final Txn transaction = getPool().getTransactionManager().beginTransaction()) {
try {
srvREST.doDelete(broker, transaction, path, request, response);
transaction.commit();
} catch (final Throwable t) {
transaction.abort();
throw t;
}
} catch (final PermissionDeniedException e) {
// Else return a FORBIDDEN Error
if (user.equals(getDefaultUser())) {
getAuthenticator().sendChallenge(request, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
}
} catch (final NotFoundException e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
} catch (final EXistException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} catch (final Throwable e) {
getLog().error(e);
throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
}
}
use of org.exist.http.Descriptor in project exist by eXist-db.
the class EXistServlet method doPut.
@Override
protected void doPut(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
// first, adjust the path
String path = adjustPath(request);
// second, perform descriptor actions
final Descriptor descriptor = Descriptor.getDescriptorSingleton();
if (descriptor != null) {
// TODO: figure out a way to log PUT requests with
// HttpServletRequestWrapper and
// Descriptor.doLogRequestInReplayLog()
// map's the path if a mapping is specified in the descriptor
path = descriptor.mapPath(path);
}
// third, authenticate the user
final Subject user = authenticate(request, response);
if (user == null) {
// You now get a HTTP Authentication challenge if there is no user
return;
}
// fourth, process the request
try (final DBBroker broker = getPool().get(Optional.of(user));
final Txn transaction = getPool().getTransactionManager().beginTransaction()) {
final XmldbURI dbpath = XmldbURI.createInternal(path);
try (final Collection collection = broker.getCollection(dbpath)) {
if (collection != null) {
transaction.abort();
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "A PUT request is not allowed against a plain collection path.");
return;
}
}
try {
srvREST.doPut(broker, transaction, dbpath, request, response);
transaction.commit();
} catch (final Throwable t) {
transaction.abort();
throw t;
}
} catch (final BadRequestException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
} catch (final PermissionDeniedException e) {
// Else return a FORBIDDEN Error
if (user.equals(getDefaultUser())) {
getAuthenticator().sendChallenge(request, response);
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
}
} catch (final EXistException e) {
if (response.isCommitted()) {
throw new ServletException(e.getMessage(), e);
}
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
} catch (final Throwable e) {
LOG.error(e);
throw new ServletException("An unknown error occurred: " + e.getMessage(), e);
}
}
Aggregations