Search in sources :

Example 81 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.

the class JspViewHandler method render.

public void render(String name, String page, String contentType, String encoding, String info, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
    if (request == null) {
        throw new ViewHandlerException("Null HttpServletRequest object");
    }
    if (UtilValidate.isEmpty(page)) {
        throw new ViewHandlerException("Null or empty source");
    }
    // Debug.logInfo("Requested Page : " + page, module);
    // Debug.logInfo("Physical Path  : " + context.getRealPath(page));
    // tell the ControlFilter we are forwarding
    request.setAttribute(ControlFilter.FORWARDED_FROM_SERVLET, Boolean.TRUE);
    RequestDispatcher rd = request.getRequestDispatcher(page);
    if (rd == null) {
        Debug.logInfo("HttpServletRequest.getRequestDispatcher() failed; trying ServletContext", module);
        rd = context.getRequestDispatcher(page);
        if (rd == null) {
            Debug.logInfo("ServletContext.getRequestDispatcher() failed; trying ServletContext.getNamedDispatcher(\"jsp\")", module);
            rd = context.getNamedDispatcher("jsp");
            if (rd == null) {
                throw new ViewHandlerException("Source returned a null dispatcher (" + page + ")");
            }
        }
    }
    try {
        rd.include(request, response);
    } catch (IOException ie) {
        throw new ViewHandlerException("IO Error in view", ie);
    } catch (ServletException e) {
        Throwable throwable = e.getRootCause() != null ? e.getRootCause() : e;
        if (throwable instanceof JspException) {
            JspException jspe = (JspException) throwable;
            throwable = jspe.getCause() != null ? jspe.getCause() : jspe;
        }
        Debug.logError(throwable, "ServletException rendering JSP view", module);
        throw new ViewHandlerException(e.getMessage(), throwable);
    }
}
Also used : ServletException(javax.servlet.ServletException) JspException(javax.servlet.jsp.JspException) IOException(java.io.IOException) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 82 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.

the class ContentUrlFilter method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    Delegator delegator = (Delegator) httpRequest.getSession().getServletContext().getAttribute("delegator");
    String urlContentId = null;
    String pathInfo = UtilHttp.getFullRequestUrl(httpRequest);
    if (UtilValidate.isNotEmpty(pathInfo)) {
        String alternativeUrl = pathInfo.substring(pathInfo.lastIndexOf('/'));
        if (alternativeUrl.endsWith("-content")) {
            try {
                GenericValue contentDataResourceView = EntityQuery.use(delegator).from("ContentDataResourceView").where("drObjectInfo", alternativeUrl).orderBy("createdDate DESC").queryFirst();
                if (contentDataResourceView != null) {
                    GenericValue content = EntityQuery.use(delegator).from("ContentAssoc").where("contentAssocTypeId", "ALTERNATIVE_URL", "contentIdTo", contentDataResourceView.get("contentId")).filterByDate().queryFirst();
                    if (content != null) {
                        urlContentId = content.getString("contentId");
                    }
                }
            } catch (GenericEntityException gee) {
                Debug.logWarning(gee.getMessage(), module);
            } catch (Exception e) {
                Debug.logWarning(e.getMessage(), module);
            }
        }
        if (UtilValidate.isNotEmpty(urlContentId)) {
            StringBuilder urlBuilder = new StringBuilder();
            urlBuilder.append("/" + WebAppUtil.CONTROL_MOUNT_POINT);
            urlBuilder.append("/" + config.getInitParameter("viewRequest") + "?contentId=" + urlContentId);
            // Set view query parameters
            UrlServletHelper.setViewQueryParameters(request, urlBuilder);
            Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module);
            RequestDispatcher dispatch = request.getRequestDispatcher(urlBuilder.toString());
            dispatch.forward(request, response);
            return;
        }
        // Check path alias
        UrlServletHelper.checkPathAlias(request, httpResponse, delegator, pathInfo);
    }
    // we're done checking; continue on
    chain.doFilter(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 83 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.

the class SeoCatalogUrlServlet method doGet.

/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Delegator delegator = (Delegator) getServletContext().getAttribute("delegator");
    String pathInfo = request.getPathInfo();
    List<String> pathElements = StringUtil.split(pathInfo, "/");
    // look for productId
    String productId = null;
    try {
        String lastPathElement = pathElements.get(pathElements.size() - 1);
        if (lastPathElement.startsWith("p_") || EntityQuery.use(delegator).from("Product").where("productId", lastPathElement).cache().queryOne() != null) {
            if (lastPathElement.startsWith("p_")) {
                productId = lastPathElement.substring(2);
            } else {
                productId = lastPathElement;
            }
            pathElements.remove(pathElements.size() - 1);
        }
    } catch (GenericEntityException e) {
        Debug.logError(e, "Error looking up product info for ProductUrl with path info [" + pathInfo + "]: " + e.toString(), module);
    }
    // get category info going with the IDs that remain
    String categoryId = null;
    if (pathElements.size() == 1) {
        CategoryWorker.setTrail(request, pathElements.get(0), null);
        categoryId = pathElements.get(0);
    } else if (pathElements.size() == 2) {
        CategoryWorker.setTrail(request, pathElements.get(1), pathElements.get(0));
        categoryId = pathElements.get(1);
    } else if (pathElements.size() > 2) {
        List<String> trail = CategoryWorker.getTrail(request);
        if (trail == null) {
            trail = new LinkedList<String>();
        }
        if (trail.contains(pathElements.get(0))) {
            // first category is in the trail, so remove it everything after that and fill it in with the list from the pathInfo
            int firstElementIndex = trail.indexOf(pathElements.get(0));
            while (trail.size() > firstElementIndex) {
                trail.remove(firstElementIndex);
            }
            trail.addAll(pathElements);
        } else {
            // first category is NOT in the trail, so clear out the trail and use the pathElements list
            trail.clear();
            trail.addAll(pathElements);
        }
        CategoryWorker.setTrail(request, trail);
        categoryId = pathElements.get(pathElements.size() - 1);
    }
    if (categoryId != null) {
        request.setAttribute("productCategoryId", categoryId);
    }
    String rootCategoryId = null;
    if (pathElements.size() >= 1) {
        rootCategoryId = pathElements.get(0);
    }
    if (rootCategoryId != null) {
        request.setAttribute("rootCategoryId", rootCategoryId);
    }
    if (productId != null) {
        request.setAttribute("product_id", productId);
        request.setAttribute("productId", productId);
    }
    RequestDispatcher rd = request.getRequestDispatcher("/" + (UtilValidate.isEmpty(SeoControlServlet.getControlServlet()) ? "" : (SeoControlServlet.getControlServlet() + "/")) + (productId != null ? PRODUCT_REQUEST : CATEGORY_REQUEST));
    rd.forward(request, response);
}
Also used : Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) RequestDispatcher(javax.servlet.RequestDispatcher)

Example 84 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.

the class CatalogUrlSeoTransform method forwardCategoryUri.

/**
 * Forward a category uri according to forward pattern regular expressions.
 * @param request
 * @param response
 * @param delegator
 * @param controlServlet
 * @return
 * @throws ServletException
 * @throws IOException
 */
public static boolean forwardCategoryUri(HttpServletRequest request, HttpServletResponse response, Delegator delegator, String controlServlet) throws ServletException, IOException {
    String pathInfo = request.getRequestURI();
    String contextPath = request.getContextPath();
    if (!isCategoryMapInitialed()) {
        initCategoryMap(request);
    }
    if (!SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
        return false;
    }
    List<String> pathElements = StringUtil.split(pathInfo, "/");
    if (UtilValidate.isEmpty(pathElements)) {
        return false;
    }
    String lastPathElement = pathElements.get(pathElements.size() - 1);
    String categoryId = null;
    if (UtilValidate.isNotEmpty(lastPathElement)) {
        if (UtilValidate.isNotEmpty(SeoConfigUtil.getCategoryUrlSuffix())) {
            if (lastPathElement.endsWith(SeoConfigUtil.getCategoryUrlSuffix())) {
                lastPathElement = lastPathElement.substring(0, lastPathElement.length() - SeoConfigUtil.getCategoryUrlSuffix().length());
            } else {
                return false;
            }
        }
        for (Entry<String, String> entry : categoryNameIdMap.entrySet()) {
            String categoryName = entry.getKey();
            if (lastPathElement.startsWith(categoryName)) {
                categoryId = entry.getValue();
                break;
            }
        }
        if (UtilValidate.isEmpty(categoryId)) {
            categoryId = lastPathElement.trim();
        }
    }
    if (UtilValidate.isNotEmpty(categoryId)) {
        request.setAttribute("productCategoryId", categoryId);
        StringBuilder urlBuilder = new StringBuilder();
        if (UtilValidate.isNotEmpty(controlServlet)) {
            urlBuilder.append("/" + controlServlet);
        }
        urlBuilder.append("/" + CatalogUrlServlet.CATEGORY_REQUEST);
        UrlServletHelper.setViewQueryParameters(request, urlBuilder);
        Debug.logInfo("[Filtered request]: " + pathInfo + " (" + urlBuilder + ")", module);
        RequestDispatcher rd = request.getRequestDispatcher(urlBuilder.toString());
        rd.forward(request, response);
        return true;
    }
    return false;
}
Also used : RequestDispatcher(javax.servlet.RequestDispatcher)

Example 85 with RequestDispatcher

use of javax.servlet.RequestDispatcher in project ofbiz-framework by apache.

the class CatalogUrlServlet method doGet.

/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Delegator delegator = (Delegator) getServletContext().getAttribute("delegator");
    String pathInfo = request.getPathInfo();
    List<String> pathElements = StringUtil.split(pathInfo, "/");
    String productId = null;
    String categoryId = null;
    if (pathElements == null) {
        RequestDispatcher rd = request.getRequestDispatcher("/" + WebAppUtil.CONTROL_MOUNT_POINT + "/main");
        rd.forward(request, response);
    } else {
        try {
            String lastPathElement = pathElements.get(pathElements.size() - 1);
            if (lastPathElement.startsWith("p_")) {
                productId = lastPathElement.substring(2);
            } else {
                GenericValue productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", lastPathElement).cache(true).queryOne();
                if (productCategory != null) {
                    categoryId = lastPathElement;
                } else {
                    productId = lastPathElement;
                }
            }
            pathElements.remove(pathElements.size() - 1);
        } catch (GenericEntityException e) {
            Debug.logError(e, "Error in looking up ProductUrl or CategoryUrl with path info [" + pathInfo + "]: " + e.toString(), module);
        }
        // get category info going with the IDs that remain
        if (pathElements.size() == 1) {
            CategoryWorker.setTrail(request, pathElements.get(0), null);
            categoryId = pathElements.get(0);
        } else if (pathElements.size() == 2) {
            CategoryWorker.setTrail(request, pathElements.get(1), pathElements.get(0));
            categoryId = pathElements.get(1);
        } else if (pathElements.size() > 2) {
            List<String> trail = CategoryWorker.getTrail(request);
            if (trail == null) {
                trail = new LinkedList<String>();
            }
            if (trail.contains(pathElements.get(0))) {
                // first category is in the trail, so remove it everything after that and fill it in with the list from the pathInfo
                int firstElementIndex = trail.indexOf(pathElements.get(0));
                while (trail.size() > firstElementIndex) {
                    trail.remove(firstElementIndex);
                }
                trail.addAll(pathElements);
            } else {
                // first category is NOT in the trail, so clear out the trail and use the pathElements list
                trail.clear();
                trail.addAll(pathElements);
            }
            CategoryWorker.setTrail(request, trail);
            categoryId = pathElements.get(pathElements.size() - 1);
        }
        if (categoryId != null) {
            request.setAttribute("productCategoryId", categoryId);
        }
        String rootCategoryId = null;
        if (pathElements.size() >= 1) {
            rootCategoryId = pathElements.get(0);
        }
        if (rootCategoryId != null) {
            request.setAttribute("rootCategoryId", rootCategoryId);
        }
        if (productId != null) {
            request.setAttribute("product_id", productId);
            request.setAttribute("productId", productId);
        }
        RequestDispatcher rd = request.getRequestDispatcher("/" + WebAppUtil.CONTROL_MOUNT_POINT + "/" + (productId != null ? PRODUCT_REQUEST : CATEGORY_REQUEST));
        rd.forward(request, response);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) RequestDispatcher(javax.servlet.RequestDispatcher)

Aggregations

RequestDispatcher (javax.servlet.RequestDispatcher)354 ServletException (javax.servlet.ServletException)98 HttpSession (javax.servlet.http.HttpSession)97 IOException (java.io.IOException)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)56 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 SQLException (java.sql.SQLException)31 User (com.zyf.bean.User)28 ServletContext (javax.servlet.ServletContext)26 Properties (java.util.Properties)14 Test (org.junit.Test)14 RelatorioDAO (br.senac.tads3.pi03b.gruposete.dao.RelatorioDAO)13 RelatorioMudancas (br.senac.tads3.pi03b.gruposete.models.RelatorioMudancas)12 PrintWriter (java.io.PrintWriter)12 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)11 Resource (org.apache.sling.api.resource.Resource)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 GenericValue (org.apache.ofbiz.entity.GenericValue)9 WebUser (org.compiere.util.WebUser)9