Search in sources :

Example 11 with HandlerException

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

the class UpdateCallerList method handleWithException.

public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
    ICallerManager mgr = null;
    String manager = null;
    try {
        manager = req.getParameter(UpdateCallerList.PARAMETER_CALLERMANAGER);
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
    if (manager == null)
        mgr = this.getRuntime().getCallerManagerFactory().getDefaultCallerManager();
    if (manager != null && manager.length() > 0)
        mgr = this.getRuntime().getCallerManagerFactory().getCallerManager(manager);
    if (mgr == null || !mgr.isActive() || !mgr.isSupported(IWriteCallerRepository.class)) {
        throw new HandlerException("Requested Callermanager does not exist or is not active.", 404);
    }
    ICallerList l;
    try {
        l = XMLSerializer.toCallerList(this.getPostData(req));
        if (l != null) {
            this.m_logger.info("Updating caller list with " + l.size() + " entries.");
            for (int i = 0, j = l.size(); i < j; i++) ((IWriteCallerRepository) mgr).updateCaller(l.get(i));
            resp.getContentStreamForWrite().close();
        } else {
            this.m_logger.severe("Invalid caller list transfered from client.");
            throw new HandlerException("Invalid caller list transfered from client.", 500);
        }
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
}
Also used : HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) ICallerList(de.janrufmonitor.framework.ICallerList) ICallerManager(de.janrufmonitor.repository.ICallerManager) HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException)

Example 12 with HandlerException

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

the class Ping method handleWithException.

public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws de.janrufmonitor.service.commons.http.handler.HandlerException {
    try {
        resp.setCode(200);
        resp.setParameter("Content-type", "text/html");
        OutputStream ps = resp.getContentStreamForWrite();
        ps.write(("<html><head><title>Ping check</title></head><body>PING OK - Service is up and running @ " + new Date().toString() + "</body></html>").getBytes());
        ps.flush();
        ps.close();
    } catch (IOException e) {
        throw new HandlerException(e.getMessage(), 500);
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
}
Also used : HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Date(java.util.Date) HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) IOException(java.io.IOException)

Example 13 with HandlerException

use of de.janrufmonitor.service.commons.http.handler.HandlerException 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)

Example 14 with HandlerException

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

the class GetCallList method handleWithException.

public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
    ICallManager mgr = null;
    String manager = null;
    boolean isCompression = false;
    try {
        manager = req.getParameter(GetCallList.PARAMETER_CALLMANAGER);
        isCompression = (req.getParameter(GetCallList.PARAMETER_COMPRESSION) != null ? req.getParameter(GetCallList.PARAMETER_COMPRESSION).equalsIgnoreCase("true") : false);
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
    if (manager == null)
        mgr = this.getRuntime().getCallManagerFactory().getDefaultCallManager();
    if (manager != null && manager.length() > 0)
        mgr = this.getRuntime().getCallManagerFactory().getCallManager(manager);
    if (mgr == null || !mgr.isActive() || !mgr.isSupported(IReadCallRepository.class)) {
        throw new HandlerException("Requested Callmanager does not exist or is not active.", 404);
    }
    ICallList l = this.getRuntime().getCallFactory().createCallList();
    String filter = null;
    try {
        filter = req.getParameter(GetCallList.PARAMETER_FILTER);
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
    String s = null;
    ISearchTerm[] searchterms = null;
    try {
        s = req.getParameter(GetCallList.PARAMETER_SEARCHTERMS);
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
    if (s != null && s.length() > 0) {
        searchterms = new SearchTermSeriarlizer().getSearchTermsFromString(s);
    }
    if (filter == null || filter.length() == 0) {
        this.m_logger.info("Filter parameter &filter= was not set.");
        if (mgr.isSupported(ISearchableCallRepository.class)) {
            l = ((ISearchableCallRepository) mgr).getCalls(getAllowedMsnFilter(req), -1, -1, searchterms);
        } else
            l = ((IReadCallRepository) mgr).getCalls(getAllowedMsnFilter(req));
    } else {
        IFilter[] f = new URLFilterManager().getFiltersFromString(filter);
        f = mergeFilters(f, getAllowedMsnFilter(req));
        if (mgr.isSupported(ISearchableCallRepository.class)) {
            l = ((ISearchableCallRepository) mgr).getCalls(f, -1, -1, searchterms);
        } else
            l = ((IReadCallRepository) mgr).getCalls(f);
    }
    try {
        String xml = XMLSerializer.toXML(l, false);
        if (isCompression && xml.length() > 1024) {
            this.m_logger.info("Compressing requested data.");
            this.m_logger.info("Uncompressed data size [bytes] : " + xml.length());
            xml = CompressBase64.compressBase64Encode(xml);
            this.m_logger.info("Compressed data size [bytes] : " + xml.length());
            resp.setParameter("Content-Type", "application/janrufmonitor-compressed");
            resp.setParameter(GetCallList.PARAMETER_COMPRESSION, "true");
        } else {
            resp.setParameter("Content-Type", "text/xml");
            resp.setParameter(GetCallList.PARAMETER_COMPRESSION, "false");
        }
        resp.setParameter("Content-Length", Long.toString(xml.length()));
        OutputStream ps = resp.getContentStreamForWrite();
        ps.write(xml.getBytes());
        ps.flush();
        ps.close();
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
}
Also used : IReadCallRepository(de.janrufmonitor.repository.types.IReadCallRepository) HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) OutputStream(java.io.OutputStream) HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) ICallManager(de.janrufmonitor.repository.ICallManager) ISearchTerm(de.janrufmonitor.repository.search.ISearchTerm) ICallList(de.janrufmonitor.framework.ICallList) IFilter(de.janrufmonitor.repository.filter.IFilter) SearchTermSeriarlizer(de.janrufmonitor.repository.search.SearchTermSeriarlizer)

Example 15 with HandlerException

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

the class GetCaller method handleWithException.

public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
    ICallerManager mgr = null;
    String manager = null;
    try {
        manager = req.getParameter(GetCaller.PARAMETER_CALLERMANAGER);
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
    if (manager == null)
        mgr = this.getRuntime().getCallerManagerFactory().getDefaultCallerManager();
    if (manager != null && manager.length() > 0)
        mgr = this.getRuntime().getCallerManagerFactory().getCallerManager(manager);
    if (mgr == null || !mgr.isActive() || !mgr.isSupported(IIdentifyCallerRepository.class)) {
        throw new HandlerException("Requested Callermanager does not exist or is not active.", 404);
    }
    String number = null;
    try {
        number = req.getParameter(GetCaller.PARAMETER_NUMBER);
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
    if (number == null || number.length() == 0) {
        this.m_logger.severe("Parameter &number= was empty or not set.");
        throw new HandlerException("Parameter &number= was empty or not set.", 404);
    }
    IPhonenumber pn = null;
    StringTokenizer st = new StringTokenizer(number, ";");
    if (st.countTokens() == 3) {
        pn = this.getRuntime().getCallerFactory().createPhonenumber(st.nextToken().trim(), st.nextToken().trim(), st.nextToken().trim());
    }
    if (st.countTokens() == 2) {
        pn = this.getRuntime().getCallerFactory().createPhonenumber(st.nextToken().trim(), "", st.nextToken().trim());
    }
    if (st.countTokens() == 1) {
        pn = this.getRuntime().getCallerFactory().createPhonenumber(st.nextToken().trim());
    }
    try {
        ICaller caller = ((IIdentifyCallerRepository) mgr).getCaller(pn);
        String xml = XMLSerializer.toXML(caller, false);
        resp.setParameter("Content-Type", "text/xml");
        resp.setParameter("Content-Length", Long.toString(xml.length()));
        OutputStream ps = resp.getContentStreamForWrite();
        ps.write(xml.getBytes());
        ps.flush();
        ps.close();
    } catch (CallerNotFoundException e) {
        throw new HandlerException(e.getMessage(), 404);
    } catch (Exception e) {
        throw new HandlerException(e.getMessage(), 500);
    }
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) StringTokenizer(java.util.StringTokenizer) OutputStream(java.io.OutputStream) CallerNotFoundException(de.janrufmonitor.repository.CallerNotFoundException) IIdentifyCallerRepository(de.janrufmonitor.repository.types.IIdentifyCallerRepository) ICallerManager(de.janrufmonitor.repository.ICallerManager) CallerNotFoundException(de.janrufmonitor.repository.CallerNotFoundException) HandlerException(de.janrufmonitor.service.commons.http.handler.HandlerException) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Aggregations

HandlerException (de.janrufmonitor.service.commons.http.handler.HandlerException)33 OutputStream (java.io.OutputStream)11 ICall (de.janrufmonitor.framework.ICall)7 Date (java.util.Date)7 ICallerManager (de.janrufmonitor.repository.ICallerManager)6 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)5 ICallManager (de.janrufmonitor.repository.ICallManager)5 IFilter (de.janrufmonitor.repository.filter.IFilter)5 ICallList (de.janrufmonitor.framework.ICallList)4 ICaller (de.janrufmonitor.framework.ICaller)4 ICallerList (de.janrufmonitor.framework.ICallerList)4 IMsn (de.janrufmonitor.framework.IMsn)4 ISearchTerm (de.janrufmonitor.repository.search.ISearchTerm)3 SearchTermSeriarlizer (de.janrufmonitor.repository.search.SearchTermSeriarlizer)3 IReadCallRepository (de.janrufmonitor.repository.types.IReadCallRepository)3 ICip (de.janrufmonitor.framework.ICip)2 IName (de.janrufmonitor.framework.IName)2 CallerNotFoundException (de.janrufmonitor.repository.CallerNotFoundException)2 MsnFilter (de.janrufmonitor.repository.filter.MsnFilter)2 IIdentifyCallerRepository (de.janrufmonitor.repository.types.IIdentifyCallerRepository)2