Search in sources :

Example 1 with WebappInfo

use of org.apache.ofbiz.base.component.ComponentConfig.WebappInfo in project ofbiz-framework by apache.

the class OfbizUrlTransform method getWriter.

@Override
@SuppressWarnings("rawtypes")
public Writer getWriter(final Writer out, Map args) {
    final StringBuilder buf = new StringBuilder();
    final boolean fullPath = checkBooleanArg(args, "fullPath", false);
    final boolean secure = checkBooleanArg(args, "secure", false);
    final boolean encode = checkBooleanArg(args, "encode", true);
    final String webSiteId = convertToString(args.get("webSiteId"));
    return new Writer(out) {

        @Override
        public void close() throws IOException {
            try {
                Environment env = Environment.getCurrentEnvironment();
                // Handle prefix.
                String prefixString = convertToString(env.getVariable("urlPrefix"));
                if (!prefixString.isEmpty()) {
                    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);
                    return;
                }
                HttpServletRequest request = FreeMarkerWorker.unwrap(env.getVariable("request"));
                // Handle web site ID.
                if (!webSiteId.isEmpty()) {
                    Delegator delegator = FreeMarkerWorker.unwrap(env.getVariable("delegator"));
                    if (request != null && delegator == null) {
                        delegator = (Delegator) request.getAttribute("delegator");
                    }
                    if (delegator == null) {
                        throw new IllegalStateException("Delegator not found");
                    }
                    WebappInfo webAppInfo = WebAppUtil.getWebappInfoFromWebsiteId(webSiteId);
                    StringBuilder newUrlBuff = new StringBuilder(250);
                    OfbizUrlBuilder builder = OfbizUrlBuilder.from(webAppInfo, delegator);
                    builder.buildFullUrl(newUrlBuff, buf.toString(), secure);
                    String newUrl = newUrlBuff.toString();
                    if (encode) {
                        newUrl = URLEncoder.encode(newUrl, "UTF-8");
                    }
                    out.write(newUrl);
                    return;
                }
                if (request != null) {
                    ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
                    HttpServletResponse response = FreeMarkerWorker.unwrap(env.getVariable("response"));
                    String requestUrl = buf.toString();
                    RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
                    out.write(rh.makeLink(request, response, requestUrl, fullPath, secure, encode));
                } else {
                    out.write(buf.toString());
                }
            } catch (Exception e) {
                Debug.logWarning(e, "Exception thrown while running ofbizUrl transform", module);
                throw new IOException(e);
            }
        }

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

        @Override
        public void write(char[] cbuf, int off, int len) {
            buf.append(cbuf, off, len);
        }
    };
}
Also used : OfbizUrlBuilder(org.apache.ofbiz.webapp.OfbizUrlBuilder) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebappInfo(org.apache.ofbiz.base.component.ComponentConfig.WebappInfo) IOException(java.io.IOException) TemplateModelException(freemarker.template.TemplateModelException) IOException(java.io.IOException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Delegator(org.apache.ofbiz.entity.Delegator) RequestHandler(org.apache.ofbiz.webapp.control.RequestHandler) Environment(freemarker.core.Environment) ServletContext(javax.servlet.ServletContext) Writer(java.io.Writer)

Example 2 with WebappInfo

use of org.apache.ofbiz.base.component.ComponentConfig.WebappInfo in project ofbiz-framework by apache.

the class LoginWorker method autoLogoutCleanCookies.

// Removes all the autoLoginCookies but if the webapp requires keeping it
public static String autoLogoutCleanCookies(GenericValue userLogin, HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    Cookie[] cookies = request.getCookies();
    if (Debug.verboseOn()) {
        Debug.logVerbose("Cookies: " + Arrays.toString(cookies), module);
    }
    if (cookies != null && userLogin != null) {
        for (Cookie autoLoginCookie : cookies) {
            String autoLoginName = autoLoginCookie.getName().replace(".autoUserLoginId", "");
            WebappInfo webappInfo = ComponentConfig.getWebappInfo("default-server", autoLoginName);
            if (webappInfo != null && !webappInfo.getKeepAutologinCookie()) {
                autoLoginCookie.setMaxAge(0);
                autoLoginCookie.setPath("/");
                response.addCookie(autoLoginCookie);
            }
        }
    }
    // remove the session attributes
    session.removeAttribute("autoUserLogin");
    session.removeAttribute("autoName");
    request.setAttribute("_AUTO_LOGIN_LOGOUT_", Boolean.TRUE);
    return "success";
}
Also used : Cookie(javax.servlet.http.Cookie) HttpSession(javax.servlet.http.HttpSession) WebappInfo(org.apache.ofbiz.base.component.ComponentConfig.WebappInfo)

Example 3 with WebappInfo

use of org.apache.ofbiz.base.component.ComponentConfig.WebappInfo in project ofbiz-framework by apache.

the class LoginWorker method getAppBarWebInfos.

/**
 * Returns a <code>Collection</code> of <code>WebappInfo</code> instances that the specified
 * user is authorized to access.
 * @param security
 * @param userLogin
 * @param serverName
 * @param menuName
 * @return A <code>Collection</code> <code>WebappInfo</code> instances that the specified
 * user is authorized to access
 */
public static Collection<ComponentConfig.WebappInfo> getAppBarWebInfos(Security security, GenericValue userLogin, String serverName, String menuName) {
    Collection<ComponentConfig.WebappInfo> allInfos = ComponentConfig.getAppBarWebInfos(serverName, menuName);
    Collection<ComponentConfig.WebappInfo> allowedInfos = new ArrayList<ComponentConfig.WebappInfo>(allInfos.size());
    for (ComponentConfig.WebappInfo info : allInfos) {
        if (hasApplicationPermission(info, security, userLogin)) {
            allowedInfos.add(info);
        }
    }
    return allowedInfos;
}
Also used : WebappInfo(org.apache.ofbiz.base.component.ComponentConfig.WebappInfo) ComponentConfig(org.apache.ofbiz.base.component.ComponentConfig) ArrayList(java.util.ArrayList) WebappInfo(org.apache.ofbiz.base.component.ComponentConfig.WebappInfo)

Example 4 with WebappInfo

use of org.apache.ofbiz.base.component.ComponentConfig.WebappInfo in project ofbiz-framework by apache.

the class NotificationServices method setBaseUrl.

/**
 * The expectation is that a lot of notification messages will include
 * a link back to one or more pages in the system, which will require knowledge
 * of the base URL to extrapolate. This method will ensure that the
 * <code>baseUrl</code> field is set in the given context.
 * <p>
 * If it has been specified a default <code>baseUrl</code> will be
 * set using a best effort approach. If it is specified in the
 * url.properties configuration files of the system, that will be
 * used, otherwise it will attempt resolve the fully qualified
 * local host name.
 * <p>
 * <i>Note:</i> I thought it might be useful to have some dynamic way
 * of extending the default properties provided by the NotificationService,
 * such as the <code>baseUrl</code>, perhaps using the standard
 * <code>ResourceBundle</code> java approach so that both classes
 * and static files may be invoked.
 *
 * @param context   The context to check and, if necessary, set the
 * <code>baseUrl</code>.
 */
public static void setBaseUrl(Delegator delegator, String webSiteId, Map<String, Object> context) {
    // If the baseUrl was not specified we can do a best effort instead
    if (!context.containsKey("baseUrl")) {
        try {
            WebappInfo webAppInfo = null;
            if (webSiteId != null) {
                webAppInfo = WebAppUtil.getWebappInfoFromWebsiteId(webSiteId);
            }
            OfbizUrlBuilder builder = OfbizUrlBuilder.from(webAppInfo, delegator);
            StringBuilder newURL = new StringBuilder();
            builder.buildHostPart(newURL, "", false);
            context.put("baseUrl", newURL.toString());
            newURL = new StringBuilder();
            builder.buildHostPart(newURL, "", true);
            context.put("baseSecureUrl", newURL.toString());
        } catch (Exception e) {
            Debug.logWarning(e, "Exception thrown while adding baseUrl to context: ", module);
        }
    }
}
Also used : OfbizUrlBuilder(org.apache.ofbiz.webapp.OfbizUrlBuilder) WebappInfo(org.apache.ofbiz.base.component.ComponentConfig.WebappInfo) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Aggregations

WebappInfo (org.apache.ofbiz.base.component.ComponentConfig.WebappInfo)4 IOException (java.io.IOException)2 OfbizUrlBuilder (org.apache.ofbiz.webapp.OfbizUrlBuilder)2 Environment (freemarker.core.Environment)1 TemplateException (freemarker.template.TemplateException)1 TemplateModelException (freemarker.template.TemplateModelException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 ServletContext (javax.servlet.ServletContext)1 Cookie (javax.servlet.http.Cookie)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 ComponentConfig (org.apache.ofbiz.base.component.ComponentConfig)1 Delegator (org.apache.ofbiz.entity.Delegator)1 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)1 RequestHandler (org.apache.ofbiz.webapp.control.RequestHandler)1