Search in sources :

Example 21 with RequestHandler

use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.

the class MacroTreeRenderer method renderImage.

public void renderImage(Appendable writer, Map<String, Object> context, ModelTree.ModelNode.Image image) throws IOException {
    if (image == null) {
        return;
    }
    HttpServletResponse response = (HttpServletResponse) context.get("response");
    HttpServletRequest request = (HttpServletRequest) context.get("request");
    String urlMode = image.getUrlMode();
    String src = image.getSrc(context);
    String id = image.getId(context);
    String style = image.getStyle(context);
    String wid = image.getWidth(context);
    String hgt = image.getHeight(context);
    String border = image.getBorder(context);
    // TODO add alt to tree images image.getAlt(context);
    String alt = "";
    boolean fullPath = false;
    boolean secure = false;
    boolean encode = false;
    String urlString = "";
    if (urlMode != null && "intra-app".equalsIgnoreCase(urlMode)) {
        if (request != null && response != null) {
            ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
            RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
            urlString = rh.makeLink(request, response, src, fullPath, secure, encode);
        } else {
            urlString = src;
        }
    } else if (urlMode != null && "content".equalsIgnoreCase(urlMode)) {
        if (request != null && response != null) {
            StringBuilder newURL = new StringBuilder();
            ContentUrlTag.appendContentPrefix(request, newURL);
            newURL.append(src);
            urlString = newURL.toString();
        }
    } else {
        urlString = src;
    }
    StringWriter sr = new StringWriter();
    sr.append("<@renderImage ");
    sr.append("src=\"");
    sr.append(src);
    sr.append("\" id=\"");
    sr.append(id);
    sr.append("\" style=\"");
    sr.append(style);
    sr.append("\" wid=\"");
    sr.append(wid);
    sr.append("\" hgt=\"");
    sr.append(hgt);
    sr.append("\" border=\"");
    sr.append(border);
    sr.append("\" alt=\"");
    sr.append(alt);
    sr.append("\" urlString=\"");
    sr.append(urlString);
    sr.append("\" />");
    executeMacro(sr.toString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) StringWriter(java.io.StringWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext)

Example 22 with RequestHandler

use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.

the class MacroMenuRenderer method createImageParameters.

// Made this a separate method so it can be externalized and reused.
private Map<String, Object> createImageParameters(Map<String, Object> context, Image image) {
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("id", image.getId(context));
    parameters.put("style", image.getStyle(context));
    parameters.put("width", image.getWidth(context));
    parameters.put("height", image.getHeight(context));
    parameters.put("border", image.getBorder(context));
    String src = image.getSrc(context);
    if (UtilValidate.isNotEmpty(src) && request != null && response != null) {
        String urlMode = image.getUrlMode();
        if ("ofbiz".equalsIgnoreCase(urlMode)) {
            boolean fullPath = false;
            boolean secure = false;
            boolean encode = false;
            ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
            RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
            src = rh.makeLink(request, response, src, fullPath, secure, encode);
        } else if ("content".equalsIgnoreCase(urlMode)) {
            StringBuilder newURL = new StringBuilder();
            ContentUrlTag.appendContentPrefix(request, newURL);
            newURL.append(src);
            src = newURL.toString();
        }
    }
    parameters.put("src", src);
    return parameters;
}
Also used : RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) HashMap(java.util.HashMap) ServletContext(javax.servlet.ServletContext)

Example 23 with RequestHandler

use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.

the class ProductSearchSession method checkDoKeywordOverride.

/**
 * A ControlServlet event method used to check to see if there is an override for any of the current keywords in the search
 */
public static final String checkDoKeywordOverride(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Map<String, Object> requestParams = UtilHttp.getParameterMap(request);
    // get the current productStoreId
    String productStoreId = ProductStoreWorker.getProductStoreId(request);
    if (productStoreId != null) {
        // get a Set of all keywords in the search, if there are any...
        Set<String> keywords = new HashSet<>();
        List<ProductSearchConstraint> constraintList = ProductSearchOptions.getConstraintList(session);
        if (constraintList != null) {
            for (ProductSearchConstraint constraint : constraintList) {
                if (constraint instanceof KeywordConstraint) {
                    KeywordConstraint keywordConstraint = (KeywordConstraint) constraint;
                    Set<String> keywordSet = keywordConstraint.makeFullKeywordSet(delegator);
                    if (keywordSet != null) {
                        keywords.addAll(keywordSet);
                    }
                }
            }
        }
        if (keywords.size() > 0) {
            List<GenericValue> productStoreKeywordOvrdList = null;
            try {
                productStoreKeywordOvrdList = EntityQuery.use(delegator).from("ProductStoreKeywordOvrd").where("productStoreId", productStoreId).orderBy("-fromDate").cache(true).filterByDate().queryList();
            } catch (GenericEntityException e) {
                Debug.logError(e, "Error reading ProductStoreKeywordOvrd list, not doing keyword override", module);
            }
            if (UtilValidate.isNotEmpty(productStoreKeywordOvrdList)) {
                for (GenericValue productStoreKeywordOvrd : productStoreKeywordOvrdList) {
                    String ovrdKeyword = productStoreKeywordOvrd.getString("keyword");
                    if (keywords.contains(ovrdKeyword)) {
                        String targetTypeEnumId = productStoreKeywordOvrd.getString("targetTypeEnumId");
                        String target = productStoreKeywordOvrd.getString("target");
                        ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
                        RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
                        if ("KOTT_PRODCAT".equals(targetTypeEnumId)) {
                            String requestName = "/category/~category_id=" + target;
                            target = rh.makeLink(request, response, requestName, false, false, false);
                        } else if ("KOTT_PRODUCT".equals(targetTypeEnumId)) {
                            String requestName = "/product/~product_id=" + target;
                            target = rh.makeLink(request, response, requestName, false, false, false);
                        } else if ("KOTT_OFBURL".equals(targetTypeEnumId)) {
                            target = rh.makeLink(request, response, target, false, false, false);
                        } else if ("KOTT_AURL".equals(targetTypeEnumId)) {
                        // do nothing, is absolute URL
                        } else {
                            Debug.logError("The targetTypeEnumId [] is not recognized, not doing keyword override", module);
                            // might as well see if there are any others...
                            continue;
                        }
                        try {
                            response.sendRedirect(target);
                            return "none";
                        } catch (IOException e) {
                            Debug.logError(e, "Could not send redirect to: " + target, module);
                            continue;
                        }
                    }
                }
            }
        }
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HttpSession(javax.servlet.http.HttpSession) IOException(java.io.IOException) Delegator(org.apache.ofbiz.entity.Delegator) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ServletContext(javax.servlet.ServletContext) KeywordConstraint(org.apache.ofbiz.product.product.ProductSearch.KeywordConstraint) HashSet(java.util.HashSet) ProductSearchConstraint(org.apache.ofbiz.product.product.ProductSearch.ProductSearchConstraint)

Example 24 with RequestHandler

use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.

the class SeoTransform method getWriter.

public Writer getWriter(final Writer out, Map args) {
    final StringBuffer buf = new StringBuffer();
    final boolean fullPath = checkArg(args, "fullPath", false);
    final boolean secure = checkArg(args, "secure", false);
    final boolean encode = checkArg(args, "encode", true);
    return new Writer(out) {

        public void write(char[] cbuf, int off, int len) {
            buf.append(cbuf, off, len);
        }

        public void flush() throws IOException {
            out.flush();
        }

        public void close() throws IOException {
            try {
                Environment env = Environment.getCurrentEnvironment();
                BeanModel req = (BeanModel) env.getVariable("request");
                BeanModel res = (BeanModel) env.getVariable("response");
                Object prefix = env.getVariable("urlPrefix");
                if (req != null) {
                    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
                    ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
                    HttpServletResponse response = null;
                    if (res != null) {
                        response = (HttpServletResponse) res.getWrappedObject();
                    }
                    HttpSession session = request.getSession();
                    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
                    // anonymous shoppers are not logged in
                    if (userLogin != null && "anonymous".equals(userLogin.getString("userLoginId"))) {
                        userLogin = null;
                    }
                    RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
                    out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure, encode), userLogin == null));
                } else if (prefix != null) {
                    if (prefix instanceof TemplateScalarModel) {
                        TemplateScalarModel s = (TemplateScalarModel) prefix;
                        String prefixString = s.getAsString();
                        String bufString = buf.toString();
                        boolean prefixSlash = prefixString.endsWith("/");
                        boolean bufSlash = bufString.startsWith("/");
                        if (prefixSlash && bufSlash) {
                            bufString = bufString.substring(1);
                        } else if (!prefixSlash && !bufSlash) {
                            bufString = "/" + bufString;
                        }
                        out.write(prefixString + bufString);
                    }
                } else {
                    out.write(buf.toString());
                }
            } catch (IOException | TemplateModelException e) {
                throw new IOException(e.getMessage());
            }
        }
    };
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) TemplateModelException(freemarker.template.TemplateModelException) BeanModel(freemarker.ext.beans.BeanModel) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) Environment(freemarker.core.Environment) ServletContext(javax.servlet.ServletContext) TemplateScalarModel(freemarker.template.TemplateScalarModel) Writer(java.io.Writer)

Example 25 with RequestHandler

use of org.apache.ofbiz.webapp.control.RequestHandler in project ofbiz-framework by apache.

the class TaskEvents method delegateAndAcceptAssignment.

/**
 * Delegate and accept assignment event
 */
public static String delegateAndAcceptAssignment(HttpServletRequest request, HttpServletResponse response) {
    ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
    RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
    Locale locale = UtilHttp.getLocale(request);
    if (addToOrderRole(request)) {
        try {
            EventHandler eh = rh.getEventFactory().getEventHandler("service");
            eh.invoke(new Event("service", "", "wfAcceptRoleAssignment", true), null, request, response);
        } catch (EventHandlerException e) {
            Debug.logError(e, "Invocation error", module);
            request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error, "OrderFailedToInvokeTheWfDelegateAndAcceptAssignmentService", locale));
            return "error";
        }
        return "success";
    }
    return "error";
}
Also used : Locale(java.util.Locale) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) EventHandlerException(org.apache.ofbiz.webapp.event.EventHandlerException) ServletContext(javax.servlet.ServletContext) EventHandler(org.apache.ofbiz.webapp.event.EventHandler) Event(org.apache.ofbiz.webapp.control.ConfigXMLReader.Event)

Aggregations

RequestHandler (org.apache.ofbiz.webapp.control.RequestHandler)27 ServletContext (javax.servlet.ServletContext)24 HttpServletRequest (javax.servlet.http.HttpServletRequest)17 HttpServletResponse (javax.servlet.http.HttpServletResponse)17 HashMap (java.util.HashMap)10 IOException (java.io.IOException)9 Writer (java.io.Writer)7 Environment (freemarker.core.Environment)6 Locale (java.util.Locale)6 WeakHashMap (java.util.WeakHashMap)6 GeneralException (org.apache.ofbiz.base.util.GeneralException)6 GenericValue (org.apache.ofbiz.entity.GenericValue)6 HttpSession (javax.servlet.http.HttpSession)5 Delegator (org.apache.ofbiz.entity.Delegator)5 Map (java.util.Map)4 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)4 TemplateModelException (freemarker.template.TemplateModelException)3 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)3 EventHandlerException (org.apache.ofbiz.webapp.event.EventHandlerException)3 BeanModel (freemarker.ext.beans.BeanModel)2