Search in sources :

Example 1 with ControllerConfig

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

the class ArtifactInfoFactory method prepareTaskForComponentAnalysis.

private Callable<Void> prepareTaskForComponentAnalysis(final ComponentConfig componentConfig) {
    return new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            String componentName = componentConfig.getGlobalName();
            String rootComponentPath = componentConfig.getRootLocation();
            List<File> screenFiles = new ArrayList<File>();
            List<File> formFiles = new ArrayList<File>();
            List<File> controllerFiles = new ArrayList<File>();
            try {
                screenFiles = FileUtil.findXmlFiles(rootComponentPath, null, "screens", "widget-screen.xsd");
            } catch (IOException ioe) {
                Debug.logWarning(ioe.getMessage(), module);
            }
            try {
                formFiles = FileUtil.findXmlFiles(rootComponentPath, null, "forms", "widget-form.xsd");
            } catch (IOException ioe) {
                Debug.logWarning(ioe.getMessage(), module);
            }
            try {
                controllerFiles = FileUtil.findXmlFiles(rootComponentPath, null, "site-conf", "site-conf.xsd");
            } catch (IOException ioe) {
                Debug.logWarning(ioe.getMessage(), module);
            }
            for (File screenFile : screenFiles) {
                String screenFilePath = screenFile.getAbsolutePath();
                screenFilePath = screenFilePath.replace('\\', '/');
                String screenFileRelativePath = screenFilePath.substring(rootComponentPath.length());
                String screenLocation = "component://" + componentName + "/" + screenFileRelativePath;
                Map<String, ModelScreen> modelScreenMap = null;
                try {
                    modelScreenMap = ScreenFactory.getScreensFromLocation(screenLocation);
                } catch (Exception exc) {
                    Debug.logWarning(exc.getMessage(), module);
                }
                for (String screenName : modelScreenMap.keySet()) {
                    getScreenWidgetArtifactInfo(screenName, screenLocation);
                }
            }
            for (File formFile : formFiles) {
                String formFilePath = formFile.getAbsolutePath();
                formFilePath = formFilePath.replace('\\', '/');
                String formFileRelativePath = formFilePath.substring(rootComponentPath.length());
                String formLocation = "component://" + componentName + "/" + formFileRelativePath;
                Map<String, ModelForm> modelFormMap = null;
                try {
                    modelFormMap = FormFactory.getFormsFromLocation(formLocation, getEntityModelReader(), getDispatchContext());
                } catch (Exception exc) {
                    Debug.logWarning(exc.getMessage(), module);
                }
                for (String formName : modelFormMap.keySet()) {
                    try {
                        getFormWidgetArtifactInfo(formName, formLocation);
                    } catch (GeneralException ge) {
                        Debug.logWarning(ge.getMessage(), module);
                    }
                }
            }
            for (File controllerFile : controllerFiles) {
                URL controllerUrl = null;
                try {
                    controllerUrl = controllerFile.toURI().toURL();
                } catch (MalformedURLException mue) {
                    Debug.logWarning(mue.getMessage(), module);
                }
                if (controllerUrl == null)
                    continue;
                ControllerConfig cc = ConfigXMLReader.getControllerConfig(controllerUrl);
                for (String requestUri : cc.getRequestMapMap().keySet()) {
                    try {
                        getControllerRequestArtifactInfo(controllerUrl, requestUri);
                    } catch (GeneralException e) {
                        Debug.logWarning(e.getMessage(), module);
                    }
                }
                for (String viewUri : cc.getViewMapMap().keySet()) {
                    try {
                        getControllerViewArtifactInfo(controllerUrl, viewUri);
                    } catch (GeneralException e) {
                        Debug.logWarning(e.getMessage(), module);
                    }
                }
            }
            return null;
        }
    };
}
Also used : MalformedURLException(java.net.MalformedURLException) ControllerConfig(org.apache.ofbiz.webapp.control.ConfigXMLReader.ControllerConfig) GeneralException(org.apache.ofbiz.base.util.GeneralException) ArrayList(java.util.ArrayList) ModelScreen(org.apache.ofbiz.widget.model.ModelScreen) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) WebAppConfigurationException(org.apache.ofbiz.webapp.control.WebAppConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) SAXException(org.xml.sax.SAXException) GeneralException(org.apache.ofbiz.base.util.GeneralException) URL(java.net.URL) File(java.io.File) ModelForm(org.apache.ofbiz.widget.model.ModelForm)

Example 2 with ControllerConfig

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

the class OfbizUrlBuilder method from.

/**
 * Returns an <code>OfbizUrlBuilder</code> instance. Use this method when you
 * don't have a <code>HttpServletRequest</code> object - like in scheduled jobs.
 *
 * @param webAppInfo Optional - if <code>null</code>, the builder can only build the host part,
 * and that will be based only on the settings in <code>url.properties</code> (the WebSite
 * entity will be ignored).
 * @param delegator
 * @throws WebAppConfigurationException
 * @throws IOException
 * @throws SAXException
 * @throws GenericEntityException
 */
public static OfbizUrlBuilder from(WebappInfo webAppInfo, Delegator delegator) throws WebAppConfigurationException, IOException, SAXException, GenericEntityException {
    WebSiteProperties webSiteProps = null;
    ControllerConfig config = null;
    String servletPath = null;
    if (webAppInfo != null) {
        Assert.notNull("delegator", delegator);
        String webSiteId = WebAppUtil.getWebSiteId(webAppInfo);
        if (webSiteId != null) {
            GenericValue webSiteValue = EntityQuery.use(delegator).from("WebSite").where("webSiteId", webSiteId).cache().queryOne();
            if (webSiteValue != null) {
                webSiteProps = WebSiteProperties.from(webSiteValue);
            }
        }
        config = ConfigXMLReader.getControllerConfig(webAppInfo);
        servletPath = WebAppUtil.getControlServletPath(webAppInfo);
    }
    if (webSiteProps == null) {
        webSiteProps = WebSiteProperties.defaults(delegator);
    }
    return new OfbizUrlBuilder(config, webSiteProps, servletPath);
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ControllerConfig(org.apache.ofbiz.webapp.control.ConfigXMLReader.ControllerConfig) WebSiteProperties(org.apache.ofbiz.webapp.website.WebSiteProperties)

Example 3 with ControllerConfig

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

the class SeoContextFilter method doFilter.

/**
 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
 */
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    String uri = httpRequest.getRequestURI();
    boolean forwarded = forwardUri(httpResponse, uri);
    if (forwarded) {
        return;
    }
    URL controllerConfigURL = ConfigXMLReader.getControllerConfigURL(config.getServletContext());
    ControllerConfig controllerConfig = null;
    Map<String, ConfigXMLReader.RequestMap> requestMaps = null;
    try {
        controllerConfig = ConfigXMLReader.getControllerConfig(controllerConfigURL);
        requestMaps = controllerConfig.getRequestMapMap();
    } catch (WebAppConfigurationException e) {
        Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
        throw new ServletException(e);
    }
    Set<String> uris = requestMaps.keySet();
    // test to see if we have come through the control servlet already, if not do the processing
    String requestPath = null;
    String contextUri = null;
    if (httpRequest.getAttribute(ControlFilter.FORWARDED_FROM_SERVLET) == null) {
        requestPath = httpRequest.getServletPath();
        if (requestPath == null)
            requestPath = "";
        if (requestPath.lastIndexOf('/') > 0) {
            if (requestPath.indexOf('/') == 0) {
                requestPath = '/' + requestPath.substring(1, requestPath.indexOf('/', 1));
            } else {
                requestPath = requestPath.substring(1, requestPath.indexOf('/'));
            }
        }
        String requestInfo = httpRequest.getServletPath();
        if (requestInfo == null)
            requestInfo = "";
        if (requestInfo.lastIndexOf('/') >= 0) {
            requestInfo = requestInfo.substring(0, requestInfo.lastIndexOf('/')) + "/*";
        }
        StringBuilder contextUriBuffer = new StringBuilder();
        if (httpRequest.getContextPath() != null) {
            contextUriBuffer.append(httpRequest.getContextPath());
        }
        if (httpRequest.getServletPath() != null) {
            contextUriBuffer.append(httpRequest.getServletPath());
        }
        if (httpRequest.getPathInfo() != null) {
            contextUriBuffer.append(httpRequest.getPathInfo());
        }
        contextUri = contextUriBuffer.toString();
        List<String> pathItemList = StringUtil.split(httpRequest.getPathInfo(), "/");
        String viewName = "";
        if (pathItemList != null) {
            viewName = pathItemList.get(0);
        }
        String requestUri = UtilHttp.getRequestUriFromTarget(httpRequest.getRequestURI());
        // check to make sure the requested url is allowed
        if (!allowedPathList.contains(requestPath) && !allowedPathList.contains(requestInfo) && !allowedPathList.contains(httpRequest.getServletPath()) && !allowedPathList.contains(requestUri) && !allowedPathList.contains("/" + viewName) && (UtilValidate.isEmpty(requestPath) && UtilValidate.isEmpty(httpRequest.getServletPath()) && !uris.contains(viewName))) {
            String filterMessage = "[Filtered request]: " + contextUri;
            if (redirectPath == null) {
                if (UtilValidate.isEmpty(viewName)) {
                    // redirect without any url change in browser
                    RequestDispatcher rd = request.getRequestDispatcher(SeoControlServlet.getDefaultPage());
                    rd.forward(request, response);
                } else {
                    int error = 404;
                    if (UtilValidate.isNotEmpty(errorCode)) {
                        try {
                            error = Integer.parseInt(errorCode);
                        } catch (NumberFormatException nfe) {
                            Debug.logWarning(nfe, "Error code specified would not parse to Integer : " + errorCode, module);
                        }
                    }
                    filterMessage = filterMessage + " (" + error + ")";
                    httpResponse.sendError(error, contextUri);
                    request.setAttribute("filterRequestUriError", contextUri);
                }
            } else {
                filterMessage = filterMessage + " (" + redirectPath + ")";
                if (!redirectPath.toLowerCase(Locale.getDefault()).startsWith("http")) {
                    redirectPath = httpRequest.getContextPath() + redirectPath;
                }
                // httpResponse.sendRedirect(redirectPath);
                if ("".equals(uri) || "/".equals(uri)) {
                    // redirect without any url change in browser
                    RequestDispatcher rd = request.getRequestDispatcher(redirectPath);
                    rd.forward(request, response);
                } else {
                    // redirect with url change in browser
                    httpResponse.setStatus(SeoConfigUtil.getDefaultResponseCode());
                    httpResponse.setHeader("Location", redirectPath);
                }
            }
            Debug.logWarning(filterMessage, module);
            return;
        } else if ((allowedPathList.contains(requestPath) || allowedPathList.contains(requestInfo) || allowedPathList.contains(httpRequest.getServletPath()) || allowedPathList.contains(requestUri) || allowedPathList.contains("/" + viewName)) && !webServlets.contains(httpRequest.getServletPath())) {
            request.setAttribute(SeoControlServlet.REQUEST_IN_ALLOW_LIST, Boolean.TRUE);
        }
    }
    // we're done checking; continue on
    chain.doFilter(httpRequest, httpResponse);
}
Also used : ControllerConfig(org.apache.ofbiz.webapp.control.ConfigXMLReader.ControllerConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) URL(java.net.URL) WebAppConfigurationException(org.apache.ofbiz.webapp.control.WebAppConfigurationException) RequestDispatcher(javax.servlet.RequestDispatcher) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException)

Example 4 with ControllerConfig

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

the class OfbizUrlBuilder method from.

/**
 * Returns an <code>OfbizUrlBuilder</code> instance.
 *
 * @param request
 * @throws GenericEntityException
 * @throws WebAppConfigurationException
 */
public static OfbizUrlBuilder from(HttpServletRequest request) throws GenericEntityException, WebAppConfigurationException {
    Assert.notNull("request", request);
    OfbizUrlBuilder builder = (OfbizUrlBuilder) request.getAttribute("_OFBIZ_URL_BUILDER_");
    if (builder == null) {
        WebSiteProperties webSiteProps = WebSiteProperties.from(request);
        URL url = ConfigXMLReader.getControllerConfigURL(request.getServletContext());
        ControllerConfig config = ConfigXMLReader.getControllerConfig(url);
        String servletPath = (String) request.getAttribute("_CONTROL_PATH_");
        builder = new OfbizUrlBuilder(config, webSiteProps, servletPath);
        request.setAttribute("_OFBIZ_URL_BUILDER_", builder);
    }
    return builder;
}
Also used : ControllerConfig(org.apache.ofbiz.webapp.control.ConfigXMLReader.ControllerConfig) WebSiteProperties(org.apache.ofbiz.webapp.website.WebSiteProperties) URL(java.net.URL)

Aggregations

ControllerConfig (org.apache.ofbiz.webapp.control.ConfigXMLReader.ControllerConfig)4 URL (java.net.URL)3 WebAppConfigurationException (org.apache.ofbiz.webapp.control.WebAppConfigurationException)2 WebSiteProperties (org.apache.ofbiz.webapp.website.WebSiteProperties)2 File (java.io.File)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 Callable (java.util.concurrent.Callable)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 GeneralException (org.apache.ofbiz.base.util.GeneralException)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)1 ModelForm (org.apache.ofbiz.widget.model.ModelForm)1 ModelScreen (org.apache.ofbiz.widget.model.ModelScreen)1