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);
}
};
}
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";
}
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;
}
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);
}
}
}
Aggregations