use of org.apache.ofbiz.webapp.website.WebSiteProperties 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);
}
use of org.apache.ofbiz.webapp.website.WebSiteProperties 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.website.WebSiteProperties 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;
}
Aggregations