use of org.apache.ofbiz.webapp.OfbizUrlBuilder 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.webapp.OfbizUrlBuilder in project ofbiz-framework by apache.
the class RequestHandler method makeLink.
public String makeLink(HttpServletRequest request, HttpServletResponse response, String url, boolean fullPath, boolean secure, boolean encode) {
WebSiteProperties webSiteProps = null;
try {
webSiteProps = WebSiteProperties.from(request);
} catch (GenericEntityException e) {
// If the entity engine is throwing exceptions, then there is no point in continuing.
Debug.logError(e, "Exception thrown while getting web site properties: ", module);
return null;
}
String requestUri = RequestHandler.getRequestUri(url);
ConfigXMLReader.RequestMap requestMap = null;
if (requestUri != null) {
try {
requestMap = getControllerConfig().getRequestMapMap().get(requestUri);
} catch (WebAppConfigurationException e) {
// If we can't read the controller.xml file, then there is no point in continuing.
Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
return null;
}
}
boolean didFullSecure = false;
boolean didFullStandard = false;
if (requestMap != null && (webSiteProps.getEnableHttps() || fullPath || secure)) {
if (Debug.verboseOn())
Debug.logVerbose("In makeLink requestUri=" + requestUri, module);
if (secure || (webSiteProps.getEnableHttps() && requestMap.securityHttps && !request.isSecure())) {
didFullSecure = true;
} else if (fullPath || (webSiteProps.getEnableHttps() && !requestMap.securityHttps && request.isSecure())) {
didFullStandard = true;
}
}
StringBuilder newURL = new StringBuilder(250);
if (didFullSecure || didFullStandard) {
// Build the scheme and host part
try {
OfbizUrlBuilder builder = OfbizUrlBuilder.from(request);
builder.buildHostPart(newURL, url, didFullSecure);
} catch (GenericEntityException e) {
// If the entity engine is throwing exceptions, then there is no point in continuing.
Debug.logError(e, "Exception thrown while getting web site properties: ", module);
return null;
} catch (WebAppConfigurationException e) {
// If we can't read the controller.xml file, then there is no point in continuing.
Debug.logError(e, "Exception thrown while parsing controller.xml file: ", module);
return null;
} catch (IOException e) {
// If we can't write to StringBuilder, then there is no point in continuing.
Debug.logError(e, "Exception thrown while writing to StringBuilder: ", module);
return null;
}
}
// create the path to the control servlet
String controlPath = (String) request.getAttribute("_CONTROL_PATH_");
// If required by webSite parameter, surcharge control path
if (webSiteProps.getWebappPath() != null) {
String requestPath = request.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("/"));
}
}
controlPath = webSiteProps.getWebappPath().concat(requestPath);
}
newURL.append(controlPath);
Delegator delegator = (Delegator) request.getAttribute("delegator");
String webSiteId = WebSiteWorker.getWebSiteId(request);
if (webSiteId != null) {
try {
GenericValue webSiteValue = EntityQuery.use(delegator).from("WebSite").where("webSiteId", webSiteId).cache().queryOne();
if (webSiteValue != null) {
ServletContext application = ((ServletContext) request.getAttribute("servletContext"));
String domainName = request.getLocalName();
if (application.getAttribute("MULTI_SITE_ENABLED") != null && UtilValidate.isNotEmpty(webSiteValue.getString("hostedPathAlias")) && !domainName.equals(webSiteValue.getString("httpHost"))) {
newURL.append('/');
newURL.append(webSiteValue.getString("hostedPathAlias"));
}
}
} catch (GenericEntityException e) {
Debug.logWarning(e, "Problems with WebSite entity", module);
}
}
// now add the actual passed url, but if it doesn't start with a / add one first
if (!url.startsWith("/")) {
newURL.append("/");
}
newURL.append(url);
String encodedUrl;
if (encode) {
encodedUrl = response.encodeURL(newURL.toString());
} else {
encodedUrl = newURL.toString();
}
return encodedUrl;
}
use of org.apache.ofbiz.webapp.OfbizUrlBuilder in project ofbiz-framework by apache.
the class CatalogAltUrlSeoTransform method getWriter.
@Override
public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException {
final StringBuilder buf = new StringBuilder();
final boolean fullPath = checkArg(args, "fullPath", false);
final boolean secure = checkArg(args, "secure", false);
return new Writer(out) {
public void write(char[] cbuf, int off, int len) throws IOException {
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");
String previousCategoryId = getStringArg(args, "previousCategoryId");
String productCategoryId = getStringArg(args, "productCategoryId");
String productId = getStringArg(args, "productId");
String url = "";
Object prefix = env.getVariable("urlPrefix");
String viewSize = getStringArg(args, "viewSize");
String viewIndex = getStringArg(args, "viewIndex");
String viewSort = getStringArg(args, "viewSort");
String searchString = getStringArg(args, "searchString");
if (req != null) {
HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
StringBuilder newURL = new StringBuilder();
if (UtilValidate.isNotEmpty(productId)) {
if (SeoConfigUtil.isCategoryUrlEnabled(request.getContextPath())) {
url = CatalogUrlSeoTransform.makeProductUrl(request, productId, productCategoryId, previousCategoryId);
} else {
url = CatalogUrlFilter.makeProductUrl(request, previousCategoryId, productCategoryId, productId);
}
} else {
if (SeoConfigUtil.isCategoryUrlEnabled(request.getContextPath())) {
url = CatalogUrlSeoTransform.makeCategoryUrl(request, productCategoryId, previousCategoryId, viewSize, viewIndex, viewSort, searchString);
} else {
url = CatalogUrlFilter.makeCategoryUrl(request, previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
}
}
// make the link
if (fullPath) {
try {
OfbizUrlBuilder builder = OfbizUrlBuilder.from(request);
builder.buildHostPart(newURL, "", secure);
} catch (WebAppConfigurationException e) {
Debug.logError(e.getMessage(), module);
}
}
newURL.append(url);
out.write(newURL.toString());
} else if (prefix != null) {
Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
Locale locale = (Locale) args.get("locale");
String prefixString = ((StringModel) prefix).getAsString();
prefixString = prefixString.replaceAll("/", "/");
String contextPath = prefixString;
int lastSlashIndex = prefixString.lastIndexOf('/');
if (lastSlashIndex > -1 && lastSlashIndex < prefixString.length()) {
contextPath = prefixString.substring(prefixString.lastIndexOf('/'));
}
if (UtilValidate.isNotEmpty(productId)) {
GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
ProductContentWrapper wrapper = new ProductContentWrapper(dispatcher, product, locale, EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator));
if (SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
url = CatalogUrlSeoTransform.makeProductUrl(delegator, wrapper, prefixString, contextPath, productCategoryId, previousCategoryId, productId);
} else {
url = CatalogUrlFilter.makeProductUrl(wrapper, null, prefixString, previousCategoryId, productCategoryId, productId);
}
} else {
GenericValue productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", productCategoryId).queryOne();
CategoryContentWrapper wrapper = new CategoryContentWrapper(dispatcher, productCategory, locale, EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator));
if (SeoConfigUtil.isCategoryUrlEnabled(contextPath)) {
url = CatalogUrlSeoTransform.makeCategoryUrl(delegator, wrapper, prefixString, productCategoryId, previousCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
} else {
url = CatalogUrlFilter.makeCategoryUrl(delegator, wrapper, null, prefixString, previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
}
}
out.write(url);
} else {
out.write(buf.toString());
}
} catch (TemplateModelException | GenericEntityException e) {
throw new IOException(e.getMessage());
}
}
};
}
use of org.apache.ofbiz.webapp.OfbizUrlBuilder in project ofbiz-framework by apache.
the class OfbizCatalogAltUrlTransform method getWriter.
@Override
public Writer getWriter(final Writer out, final Map args) throws TemplateModelException, IOException {
final StringBuilder buf = new StringBuilder();
final boolean fullPath = checkArg(args, "fullPath", false);
final boolean secure = checkArg(args, "secure", false);
return new Writer(out) {
@Override
public void write(char[] cbuf, int off, int len) throws IOException {
buf.append(cbuf, off, len);
}
@Override
public void flush() throws IOException {
out.flush();
}
@Override
public void close() throws IOException {
try {
Environment env = Environment.getCurrentEnvironment();
BeanModel req = (BeanModel) env.getVariable("request");
String previousCategoryId = getStringArg(args, "previousCategoryId");
String productCategoryId = getStringArg(args, "productCategoryId");
String productId = getStringArg(args, "productId");
String url = "";
Object prefix = env.getVariable("urlPrefix");
String viewSize = getStringArg(args, "viewSize");
String viewIndex = getStringArg(args, "viewIndex");
String viewSort = getStringArg(args, "viewSort");
String searchString = getStringArg(args, "searchString");
if (req != null) {
HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
StringBuilder newURL = new StringBuilder();
if (UtilValidate.isNotEmpty(productId)) {
url = CatalogUrlFilter.makeProductUrl(request, previousCategoryId, productCategoryId, productId);
} else {
url = CatalogUrlFilter.makeCategoryUrl(request, previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
}
// make the link
if (fullPath) {
OfbizUrlBuilder builder = OfbizUrlBuilder.from(request);
builder.buildHostPart(newURL, url, secure);
}
newURL.append(url);
out.write(newURL.toString());
} else if (prefix != null) {
Delegator delegator = FreeMarkerWorker.getWrappedObject("delegator", env);
LocalDispatcher dispatcher = FreeMarkerWorker.getWrappedObject("dispatcher", env);
Locale locale = (Locale) args.get("locale");
if (UtilValidate.isNotEmpty(productId)) {
GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
ProductContentWrapper wrapper = new ProductContentWrapper(dispatcher, product, locale, EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator));
url = CatalogUrlFilter.makeProductUrl(wrapper, null, ((StringModel) prefix).getAsString(), previousCategoryId, productCategoryId, productId);
} else {
GenericValue productCategory = EntityQuery.use(delegator).from("ProductCategory").where("productCategoryId", productCategoryId).queryOne();
CategoryContentWrapper wrapper = new CategoryContentWrapper(dispatcher, productCategory, locale, EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator));
url = CatalogUrlFilter.makeCategoryUrl(delegator, wrapper, null, ((StringModel) prefix).getAsString(), previousCategoryId, productCategoryId, productId, viewSize, viewIndex, viewSort, searchString);
}
out.write(url);
} else {
out.write(buf.toString());
}
} catch (TemplateModelException | GenericEntityException | WebAppConfigurationException e) {
throw new IOException(e.getMessage());
}
}
};
}
use of org.apache.ofbiz.webapp.OfbizUrlBuilder 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