Package gov.nysenate.openleg.api.AbstractApiRequest

Examples of gov.nysenate.openleg.api.AbstractApiRequest.ApiRequestException


                    m.group(SEARCH_PAGE_SIZE));
        }

        try {
            if(apiRequest == null)
                throw new ApiRequestException(TextFormatter.append("Failed to route request: ", uri));

            apiRequest.execute();
        }
        catch(ApiRequestException e) {
            logger.error(e);
View Full Code Here


        try {
            try {
                if (pageIdxParam != null) {
                    pageIdx = Integer.parseInt(pageIdxParam);
                    if (pageIdx < 1) {
                        throw new ApiRequestException("Page number must be greater than 0");
                    }

                }

                if (pageSizeParam != null) {
                    pageSize = Integer.parseInt(pageSizeParam);
                    if (pageSize > MAX_PAGE_SIZE) {
                        throw new ApiRequestException("Page size must be less than 1000");
                    }
                    else if (pageSize < 1) {
                        throw new ApiRequestException("Page size must be greater than 0");
                    }
                }
            }
            catch (NumberFormatException e) {
                throw new ApiRequestException("Invalid pageIdx ["+pageIdxParam+"] or pageSize ["+pageSizeParam+"]. Must be an integer.");
            }

            if ("true".equals(sortOrderParam)) {
                sortOrder = true;
            }
            else if (sortOrderParam == null || "false".equals(sortOrderParam)) {
                sortOrder = false;
            }
            else {
                throw new ApiRequestException("Invalid sortOrder parameter: "+sortOrderParam);
            }

            Matcher searchMatcher = searchPattern.matcher(path);
            Matcher documentMatcher = documentPattern.matcher(path);
            if (searchMatcher.find()) {
                String format = searchMatcher.group(1);
                String type = searchMatcher.group(2);
                String uriTerm = searchMatcher.group(3);
                String pagePart = searchMatcher.group(4);
                String sizePart = searchMatcher.group(5);
                String term = RequestUtils.getSearchString(request, uriTerm);

                if (pagePart != null) {
                    pageIdx = Integer.valueOf(pagePart);
                    if (pageIdx < 1) {
                        throw new ApiRequestException("Page number must be greater than 0");
                    }
                }
                if (sizePart != null) {
                    pageSize = Integer.valueOf(sizePart);
                    if (pageSize > MAX_PAGE_SIZE) {
                        throw new ApiRequestException("Page size must be less than 1000");
                    }
                    else if (pageSize < 1) {
                        throw new ApiRequestException("Page size must be greater than 0");
                    }
                }

                if (!type.equals("search")) {
                    if (type.equals("sponsor")) {
                        term = "sponsor:"+uriTerm+" AND otype:bill";
                        String filter = RequestUtils.getSearchString(request, "");
                        if (!filter.isEmpty()) {
                            term += " AND "+filter;
                        }
                    }
                    else {
                        term = "otype:"+type.substring(0, type.length()-1)+(term.isEmpty() ? "" : " AND "+term);
                    }
                }
                else if (term.isEmpty()) {
                    throw new ApiRequestException("A search term is required.");
                }

                doSearch(request, response, format, type, term, pageIdx, pageSize, sort, sortOrder);
            }
            else if (documentMatcher.find()) {
                String format = documentMatcher.group(1);
                String otype = documentMatcher.group(2);
                String oid = documentMatcher.group(3);
                doSingleView(request, response, format, otype, oid);
            }
            else {
                throw new ApiRequestException("Invalid request: "+uri);
            }
        }
        catch (ApiRequestException e) {
            logger.error(e.getMessage(), e);
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
View Full Code Here

                    PrintWriter out = response.getWriter();
                    response.setContentType("application/javascript");
                    out.write(callback+"("+new Api1JsonConverter().toString(sr)+");");
                }
                else {
                    throw new ApiRequestException("callback parameter required for jsonp queries.");
                }
            }
            else if (format.equals("xml")) {
                response.setContentType("application/xml");
                new Api1XmlConverter().write(sr, response.getOutputStream());
            }
            else if (format.equals("rss")) {
                request.setAttribute("term", term);
                request.setAttribute("results", sr);
                request.setAttribute("pageSize", pageSize);
                request.setAttribute("pageIdx", pageNumber);
                response.setContentType("application/rss+xml");
                request.getSession().getServletContext().getRequestDispatcher("/views/search-rss.jsp").forward(request, response);
            }
            else if (format.equals("atom")) {
                request.setAttribute("term", term);
                request.setAttribute("results", sr);
                request.setAttribute("pageSize", pageSize);
                request.setAttribute("pageIdx", pageNumber);
                response.setContentType("application/atom+xml");
                request.getSession().getServletContext().getRequestDispatcher("/views/search-atom.jsp").forward(request, response);
            }
            else if (format.equals("csv")) {
                request.setAttribute("term", term);
                request.setAttribute("results", sr);
                request.setAttribute("pageSize", pageSize);
                request.setAttribute("pageIdx", pageNumber);
                response.setContentType("text/plain");
                request.getSession().getServletContext().getRequestDispatcher("/views/search-csv.jsp").forward(request, response);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ApiRequestException("internal server error.");
        }
    }
View Full Code Here

    private void doSingleView(HttpServletRequest request, HttpServletResponse response, String format, String type, String id) throws ApiRequestException, IOException, ServletException
    {
        BaseObject object = (BaseObject)Application.getLucene().getSenateObject(id, type);

        if(object == null) {
            throw new ApiRequestException(TextFormatter.append("couldn't find id: ", id, " of type: ", type));
        }
        else {
            if (format.equals("json")) {
                response.setContentType("application/json");
                new Api1JsonConverter().write(object, response.getOutputStream());
            }
            else if (format.equals("jsonp")) {
                String callback = request.getParameter("callback");
                if (callback != null && callback != "") {
                    PrintWriter out = response.getWriter();
                    response.setContentType("application/javascript");
                    out.write(callback+"("+new Api1JsonConverter().toString(object)+");");
                }
                else {
                    throw new ApiRequestException("callback parameter required for jsonp queries.");
                }
            }
            else if (format.equals("xml")) {
                response.setContentType("application/xml");
                new Api1XmlConverter().write(object, response.getOutputStream());
            }
            else if (format.equals("lrs-print")) {
                request.setAttribute("bill", object);
                request.getSession().getServletContext().getRequestDispatcher("/views/bill-lrs-print.jsp").forward(request, response);
            }
            else if (format.equals("pdf")) {
                response.setContentType("application/pdf");
                try {
                    BillTextPDFConverter.write(object, response.getOutputStream());
                } catch (COSVisitorException e) {
                    logger.error(e.getMessage(), e);
                    throw new ApiRequestException("internal server error.", e);
                }
            }
            else if (format.equals("html") || format.equals("html-print")) {
                // TODO: Send a 301 response instead.
                response.sendRedirect(request.getContextPath()+"/"+type+"/"+id );
View Full Code Here

        try {
            try {
                if (pageIdxParam != null) {
                    pageIdx = Integer.parseInt(pageIdxParam);
                    if (pageIdx < 1) {
                        throw new ApiRequestException("Page number must be greater than 0");
                    }
                }

                if (pageSizeParam != null) {
                    pageSize = Integer.parseInt(pageSizeParam);
                    if (pageSize > MAX_PAGE_SIZE) {
                        throw new ApiRequestException("Page size must be less than 1000");
                    }
                    else if (pageSize < 1) {
                        throw new ApiRequestException("Page size must be greater than 0");
                    }
                }
            }
            catch (NumberFormatException e) {
                throw new ApiRequestException("Invalid pageIdx ["+pageIdxParam+"] or pageSize ["+pageSizeParam+"]. Must be an integer.");
            }

            if ("true".equals(sortOrderParam)) {
                sortOrder = true;
            }
            else if (sortOrderParam == null || "false".equals(sortOrderParam)) {
                sortOrder = false;
            }
            else {
                throw new ApiRequestException("Invalid sortOrder parameter: "+sortOrderParam);
            }

            Matcher searchMatcher = searchPattern.matcher(path);
            Matcher documentMatcher = documentPattern.matcher(path);
            if (searchMatcher.find()) {
                String type = searchMatcher.group(1);
                String format = searchMatcher.group(2);
                String term = request.getParameter("term");

                if (!type.equals("search")) {
                    term = "otype:"+type.substring(0, type.length()-1)+(term == null ? "" : " AND "+term);
                }

                doSearch(request, response, format, type, term, pageIdx, pageSize, sort, sortOrder);
            }
            else if (documentMatcher.find()) {
                String otype = documentMatcher.group(1);
                String oid = documentMatcher.group(2);
                String format = documentMatcher.group(3);
                doSingleView(request, response, format, otype, oid);
            }
            else {
                throw new ApiRequestException("Invalid request: "+uri);
            }
        }
        catch (ApiRequestException e) {
            logger.error(e.getMessage());
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
View Full Code Here

                    PrintWriter out = response.getWriter();
                    response.setContentType("application/javascript");
                    out.write(callback+"("+new Api2JsonConverter().toString(sr)+");");
                }
                else {
                    throw new ApiRequestException("callback parameter required for jsonp queries.");
                }
            }
            else if (format.equals("xml")) {
                response.setContentType("application/xml");
                new Api2XmlConverter().write(sr, response.getOutputStream());
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ApiRequestException("internal server error.");
        }
    }
View Full Code Here

                    PrintWriter out = response.getWriter();
                    response.setContentType("application/javascript");
                    out.write(callback+"("+new Api2JsonConverter().toString(sr)+");");
                }
                else {
                    throw new ApiRequestException("callback parameter required for jsonp queries.");
                }
            }
            else if (format.equals("xml")) {
                response.setContentType("application/xml");
                new Api2XmlConverter().write(sr, response.getOutputStream());
            }
            else if (format.equals("pdf")) {
                if (sr.getResults().size() == 0) {
                    throw new ApiRequestException("No matching document could be found.");
                }

                response.setContentType("application/pdf");
                PDFConverter.write(sr.getResults().get(0).object, response.getOutputStream());
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ApiRequestException("internal server error.", e);
        }
    }
View Full Code Here

    {
        if (object instanceof Transcript) {
            PDFConverter.write((Transcript)object, out);
        }
        else {
            throw new ApiRequestException("Unable to convert "+object.getOtype()+"s to pdf.");
        }
    }
View Full Code Here

TOP

Related Classes of gov.nysenate.openleg.api.AbstractApiRequest.ApiRequestException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.