Search in sources :

Example 1 with RequestHandler

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

the class HtmlMenuRenderer method renderImage.

public void renderImage(Appendable writer, Map<String, Object> context, Image image) throws IOException {
    // open tag
    writer.append("<img ");
    String id = image.getId(context);
    if (UtilValidate.isNotEmpty(id)) {
        writer.append(" id=\"");
        writer.append(id);
        writer.append("\"");
    }
    String style = image.getStyle(context);
    if (UtilValidate.isNotEmpty(style)) {
        writer.append(" class=\"");
        writer.append(style);
        writer.append("\"");
    }
    String wid = image.getWidth(context);
    if (UtilValidate.isNotEmpty(wid)) {
        writer.append(" width=\"");
        writer.append(wid);
        writer.append("\"");
    }
    String hgt = image.getHeight(context);
    if (UtilValidate.isNotEmpty(hgt)) {
        writer.append(" height=\"");
        writer.append(hgt);
        writer.append("\"");
    }
    String border = image.getBorder(context);
    if (UtilValidate.isNotEmpty(border)) {
        writer.append(" border=\"");
        writer.append(border);
        writer.append("\"");
    }
    String src = image.getSrc(context);
    if (UtilValidate.isNotEmpty(src)) {
        writer.append(" src=\"");
        String urlMode = image.getUrlMode();
        boolean fullPath = false;
        boolean secure = false;
        boolean encode = false;
        HttpServletResponse response = (HttpServletResponse) context.get("response");
        HttpServletRequest request = (HttpServletRequest) context.get("request");
        if (urlMode != null && "ofbiz".equalsIgnoreCase(urlMode)) {
            if (request != null && response != null) {
                ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
                RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
                String urlString = rh.makeLink(request, response, src, fullPath, secure, encode);
                writer.append(urlString);
            } else {
                writer.append(src);
            }
        } else if (urlMode != null && "content".equalsIgnoreCase(urlMode)) {
            if (request != null && response != null) {
                StringBuilder newURL = new StringBuilder();
                ContentUrlTag.appendContentPrefix(request, newURL);
                newURL.append(src);
                writer.append(newURL.toString());
            }
        } else {
            writer.append(src);
        }
        writer.append("\"");
    }
    writer.append("/>");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext)

Example 2 with RequestHandler

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

the class HtmlMenuRenderer method appendOfbizUrl.

public void appendOfbizUrl(Appendable writer, String location) throws IOException {
    ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
    if (ctx == null) {
        HttpSession session = request.getSession();
        if (session != null) {
            ctx = session.getServletContext();
        }
        if (ctx == null) {
            throw new RuntimeException("ctx is null. location:" + location);
        }
    }
    RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
    // make and append the link
    String s = rh.makeLink(this.request, this.response, location);
    writer.append(s);
}
Also used : RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) HttpSession(javax.servlet.http.HttpSession) ServletContext(javax.servlet.ServletContext)

Example 3 with RequestHandler

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

the class HtmlTreeRenderer method renderImage.

public void renderImage(Appendable writer, Map<String, Object> context, ModelTree.ModelNode.Image image) throws IOException {
    // open tag
    writer.append("<img ");
    String id = image.getId(context);
    if (UtilValidate.isNotEmpty(id)) {
        writer.append(" id=\"");
        writer.append(id);
        writer.append("\"");
    }
    String style = image.getStyle(context);
    if (UtilValidate.isNotEmpty(style)) {
        writer.append(" class=\"");
        writer.append(style);
        writer.append("\"");
    }
    String wid = image.getWidth(context);
    if (UtilValidate.isNotEmpty(wid)) {
        writer.append(" width=\"");
        writer.append(wid);
        writer.append("\"");
    }
    String hgt = image.getHeight(context);
    if (UtilValidate.isNotEmpty(hgt)) {
        writer.append(" height=\"");
        writer.append(hgt);
        writer.append("\"");
    }
    String border = image.getBorder(context);
    if (UtilValidate.isNotEmpty(border)) {
        writer.append(" border=\"");
        writer.append(border);
        writer.append("\"");
    }
    String src = image.getSrc(context);
    if (UtilValidate.isNotEmpty(src)) {
        writer.append(" src=\"");
        String urlMode = image.getUrlMode();
        boolean fullPath = false;
        boolean secure = false;
        boolean encode = false;
        HttpServletResponse response = (HttpServletResponse) context.get("response");
        HttpServletRequest request = (HttpServletRequest) context.get("request");
        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_");
                String urlString = rh.makeLink(request, response, src, fullPath, secure, encode);
                writer.append(urlString);
            } else {
                writer.append(src);
            }
        } else if (urlMode != null && "content".equalsIgnoreCase(urlMode)) {
            if (request != null && response != null) {
                StringBuilder newURL = new StringBuilder();
                ContentUrlTag.appendContentPrefix(request, newURL);
                newURL.append(src);
                writer.append(newURL.toString());
            }
        } else {
            writer.append(src);
        }
        writer.append("\"");
    }
    writer.append("/>");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext)

Example 4 with RequestHandler

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

the class MacroScreenRenderer method renderScreenletPaginateMenu.

protected void renderScreenletPaginateMenu(Appendable writer, Map<String, Object> context, ModelScreenWidget.Form form) throws IOException {
    HttpServletResponse response = (HttpServletResponse) context.get("response");
    HttpServletRequest request = (HttpServletRequest) context.get("request");
    ModelForm modelForm;
    try {
        modelForm = form.getModelForm(context);
    } catch (Exception e) {
        throw new IOException(e);
    }
    modelForm.runFormActions(context);
    Paginator.preparePager(modelForm, context);
    String targetService = modelForm.getPaginateTarget(context);
    if (targetService == null) {
        targetService = "${targetService}";
    }
    // get the parametrized pagination index and size fields
    int paginatorNumber = WidgetWorker.getPaginatorNumber(context);
    String viewIndexParam = modelForm.getMultiPaginateIndexField(context);
    String viewSizeParam = modelForm.getMultiPaginateSizeField(context);
    int viewIndex = Paginator.getViewIndex(modelForm, context);
    int viewSize = Paginator.getViewSize(modelForm, context);
    int listSize = Paginator.getListSize(context);
    int highIndex = Paginator.getHighIndex(context);
    int actualPageSize = Paginator.getActualPageSize(context);
    // if this is all there seems to be (if listSize < 0, then size is unknown)
    if (actualPageSize >= listSize && listSize >= 0) {
        return;
    }
    // needed for the "Page" and "rows" labels
    Map<String, String> uiLabelMap = UtilGenerics.cast(context.get("uiLabelMap"));
    String ofLabel = "";
    if (uiLabelMap == null) {
        Debug.logWarning("Could not find uiLabelMap in context", module);
    } else {
        ofLabel = uiLabelMap.get("CommonOf");
        ofLabel = ofLabel.toLowerCase(Locale.getDefault());
    }
    // for legacy support, the viewSizeParam is VIEW_SIZE and viewIndexParam is VIEW_INDEX when the fields are "viewSize" and "viewIndex"
    if (("viewIndex" + "_" + paginatorNumber).equals(viewIndexParam)) {
        viewIndexParam = "VIEW_INDEX" + "_" + paginatorNumber;
    }
    if (("viewSize" + "_" + paginatorNumber).equals(viewSizeParam)) {
        viewSizeParam = "VIEW_SIZE" + "_" + paginatorNumber;
    }
    ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
    RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
    Map<String, Object> inputFields = UtilGenerics.toMap(context.get("requestParameters"));
    // strip out any multi form fields if the form is of type multi
    if ("multi".equals(modelForm.getType())) {
        inputFields = UtilHttp.removeMultiFormParameters(inputFields);
    }
    String queryString = UtilHttp.urlEncodeArgs(inputFields);
    // strip legacy viewIndex/viewSize params from the query string
    queryString = UtilHttp.stripViewParamsFromQueryString(queryString, "" + paginatorNumber);
    // strip parametrized index/size params from the query string
    HashSet<String> paramNames = new HashSet<>();
    paramNames.add(viewIndexParam);
    paramNames.add(viewSizeParam);
    queryString = UtilHttp.stripNamedParamsFromQueryString(queryString, paramNames);
    String anchor = "";
    String paginateAnchor = modelForm.getPaginateTargetAnchor();
    if (paginateAnchor != null) {
        anchor = "#" + paginateAnchor;
    }
    // preparing the link text, so that later in the code we can reuse this and just add the viewIndex
    String prepLinkText = "";
    prepLinkText = targetService;
    if (prepLinkText.indexOf('?') < 0) {
        prepLinkText += "?";
    } else if (!prepLinkText.endsWith("?")) {
        prepLinkText += "&amp;";
    }
    if (UtilValidate.isNotEmpty(queryString) && !"null".equals(queryString)) {
        prepLinkText += queryString + "&amp;";
    }
    prepLinkText += viewSizeParam + "=" + viewSize + "&amp;" + viewIndexParam + "=";
    String linkText;
    // The current screenlet title bar navigation syling requires rendering
    // these links in reverse order
    // Last button
    String lastLinkUrl = "";
    if (highIndex < listSize) {
        int lastIndex = UtilMisc.getViewLastIndex(listSize, viewSize);
        linkText = prepLinkText + lastIndex + anchor;
        lastLinkUrl = rh.makeLink(request, response, linkText);
    }
    String nextLinkUrl = "";
    if (highIndex < listSize) {
        linkText = prepLinkText + (viewIndex + 1) + anchor;
        // - make the link
        nextLinkUrl = rh.makeLink(request, response, linkText);
    }
    String previousLinkUrl = "";
    if (viewIndex > 0) {
        linkText = prepLinkText + (viewIndex - 1) + anchor;
        previousLinkUrl = rh.makeLink(request, response, linkText);
    }
    String firstLinkUrl = "";
    if (viewIndex > 0) {
        linkText = prepLinkText + 0 + anchor;
        firstLinkUrl = rh.makeLink(request, response, linkText);
    }
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("lowIndex", Paginator.getLowIndex(context));
    parameters.put("actualPageSize", actualPageSize);
    parameters.put("ofLabel", ofLabel);
    parameters.put("listSize", listSize);
    parameters.put("paginateLastStyle", modelForm.getPaginateLastStyle());
    parameters.put("lastLinkUrl", lastLinkUrl);
    parameters.put("paginateLastLabel", modelForm.getPaginateLastLabel(context));
    parameters.put("paginateNextStyle", modelForm.getPaginateNextStyle());
    parameters.put("nextLinkUrl", nextLinkUrl);
    parameters.put("paginateNextLabel", modelForm.getPaginateNextLabel(context));
    parameters.put("paginatePreviousStyle", modelForm.getPaginatePreviousStyle());
    parameters.put("paginatePreviousLabel", modelForm.getPaginatePreviousLabel(context));
    parameters.put("previousLinkUrl", previousLinkUrl);
    parameters.put("paginateFirstStyle", modelForm.getPaginateFirstStyle());
    parameters.put("paginateFirstLabel", modelForm.getPaginateFirstLabel(context));
    parameters.put("firstLinkUrl", firstLinkUrl);
    executeMacro(writer, "renderScreenletPaginateMenu", parameters);
}
Also used : HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) GeneralException(org.apache.ofbiz.base.util.GeneralException) HttpServletRequest(javax.servlet.http.HttpServletRequest) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) ServletContext(javax.servlet.ServletContext) ModelForm(org.apache.ofbiz.widget.model.ModelForm) HashSet(java.util.HashSet)

Example 5 with RequestHandler

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

the class RomeEventHandler method invoke.

/**
 * @see org.apache.ofbiz.webapp.event.EventHandler#invoke(ConfigXMLReader.Event, ConfigXMLReader.RequestMap, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
    RequestHandler handler = (RequestHandler) request.getSession().getServletContext().getAttribute("_REQUEST_HANDLER_");
    if (handler == null) {
        throw new EventHandlerException("No request handler found in servlet context!");
    }
    // generate the main and entry links
    String entryLinkReq = request.getParameter("entryLinkReq");
    String mainLinkReq = request.getParameter("mainLinkReq");
    // create the links; but the query string must be created by the service
    String entryLink = handler.makeLink(request, response, entryLinkReq, true, false, false);
    String mainLink = handler.makeLink(request, response, mainLinkReq, true, false, false);
    request.setAttribute("entryLink", entryLink);
    request.setAttribute("mainLink", mainLink);
    String feedType = request.getParameter("feedType");
    if (feedType == null) {
        request.setAttribute("feedType", defaultFeedType);
    }
    // invoke the feed generator service (implements rssFeedInterface)
    String respCode = service.invoke(event, requestMap, request, response);
    // pull out the RSS feed from the request attributes
    WireFeed wireFeed = (WireFeed) request.getAttribute("wireFeed");
    response.setContentType(mime);
    try {
        out.output(wireFeed, response.getWriter());
    } catch (IOException e) {
        throw new EventHandlerException("Unable to get response writer", e);
    } catch (FeedException e) {
        throw new EventHandlerException("Unable to write RSS feed", e);
    }
    return respCode;
}
Also used : RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) FeedException(com.sun.syndication.io.FeedException) IOException(java.io.IOException) WireFeed(com.sun.syndication.feed.WireFeed)

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