Search in sources :

Example 1 with Handler

use of de.janrufmonitor.service.commons.http.handler.Handler in project janrufmonitor by tbrandt77.

the class ClientHandler method handleWithException.

public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
    this.m_logger.info("Processing incoming server request ...");
    try {
        resp.setParameter("Server", "jAnrufmonitor-Client-" + InetAddress.getLocalHost().getHostName() + "/2.0");
    } catch (UnknownHostException e) {
        throw new HandlerException(e.getMessage(), 500);
    }
    resp.setParameter("Date", Long.toString(System.currentTimeMillis()));
    String actionHandler = null;
    try {
        actionHandler = this.getActionHandler(req);
    } catch (Exception e) {
        throw new HandlerException("IOException in retrieving action handler", 500);
    }
    if (actionHandler == null)
        throw new HandlerException("No valid action parameter found", 404);
    if (actionHandler.length() > 0) {
        try {
            Class handler = Thread.currentThread().getContextClassLoader().loadClass(actionHandler);
            Object o = handler.newInstance();
            if (o instanceof Handler) {
                ((Handler) o).handleWithException(req, resp);
            }
        } catch (ClassNotFoundException ex) {
            throw new HandlerException("Class not found: " + actionHandler, 500);
        } catch (InstantiationException e) {
            throw new HandlerException("Cannot instantiate class: " + actionHandler, 500);
        } catch (IllegalAccessException e) {
            throw new HandlerException("Illegal access for class: " + actionHandler, 500);
        } catch (HandlerException e) {
            throw e;
        } catch (Exception e) {
            throw new HandlerException(e.getMessage(), 500);
        }
    }
    super.handleWithException(req, resp);
    this.m_logger.info("Finishing incoming server request ...");
}
Also used : HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) UnknownHostException(java.net.UnknownHostException) GenericHandler(de.janrufmonitor.service.commons.http.simple.handler.GenericHandler) Handler(de.janrufmonitor.service.commons.http.handler.Handler) HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) UnknownHostException(java.net.UnknownHostException)

Example 2 with Handler

use of de.janrufmonitor.service.commons.http.handler.Handler in project janrufmonitor by tbrandt77.

the class ServerHandler method handleWithException.

public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
    resp.setParameter("Server", "jAnrufmonitor/5.0");
    resp.setParameter("Date", Long.toString(System.currentTimeMillis()));
    try {
        // do security checks
        if (!this.isAllowed(req))
            throw new HandlerException("Access denied by IP address", 403);
    } catch (Exception e) {
        if (e instanceof HandlerException)
            throw (HandlerException) e;
        throw new HandlerException("Exception in security check.", 500);
    }
    String actionHandler = null;
    try {
        actionHandler = this.getActionHandler(req);
    } catch (Exception e) {
        throw new HandlerException("Exception in retrieving action handler", 500);
    }
    if (actionHandler == null)
        throw new HandlerException("No valid action parameter found", 404);
    if (actionHandler.length() > 0) {
        try {
            Class handler = Thread.currentThread().getContextClassLoader().loadClass(actionHandler);
            Object o = handler.newInstance();
            if (o instanceof Handler) {
                this.setHistoryEvent(req);
                this.setTransferedBytes(req);
                this.m_logger.info("Handle request " + req.getURI());
                ((Handler) o).handleWithException(req, resp);
            }
        } catch (ClassNotFoundException ex) {
            throw new HandlerException("Class not found: " + actionHandler, 500);
        } catch (InstantiationException e) {
            throw new HandlerException("Cannot instantiate class: " + actionHandler, 500);
        } catch (IllegalAccessException e) {
            throw new HandlerException("Illegal access for class: " + actionHandler, 500);
        } catch (HandlerException e) {
            throw e;
        } catch (Exception e) {
            throw new HandlerException(e.getMessage(), 500);
        }
    }
    super.handleWithException(req, resp);
}
Also used : HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) GenericHandler(de.janrufmonitor.service.commons.http.simple.handler.GenericHandler) Handler(de.janrufmonitor.service.commons.http.handler.Handler) HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException)

Aggregations

Handler (de.janrufmonitor.service.commons.http.handler.Handler)2 HandlerException (de.janrufmonitor.service.commons.http.handler.HandlerException)2 GenericHandler (de.janrufmonitor.service.commons.http.simple.handler.GenericHandler)2 UnknownHostException (java.net.UnknownHostException)1