Skip to content
Permalink
Browse files

Fixed: Merge UrlRegexpTransform and OfbizUrlTransform classes

(OFBIZ-11229)

OFBIZ-4361 depends on this issue. Here is a first version, the main identified 
point was when the request isn't present (email send, service call) the website 
isn't pull from context. This solves this aspect

Thanks: Nicolas for the patch

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1868395 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
JacquesLeRoux committed Oct 13, 2019
1 parent 940e386 commit 2f3fd5a62f23064662a71c5efecf703e7a1fc612
@@ -20,19 +20,28 @@

import java.io.IOException;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ofbiz.base.component.ComponentConfig;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.template.FreeMarkerWorker;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.product.category.SeoConfigUtil;
import org.apache.ofbiz.webapp.OfbizUrlBuilder;
import org.apache.ofbiz.webapp.WebAppUtil;
import org.apache.ofbiz.webapp.control.RequestHandler;
import org.apache.ofbiz.webapp.control.WebAppConfigurationException;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.Perl5Matcher;
import org.xml.sax.SAXException;

import freemarker.core.Environment;
import freemarker.ext.beans.BeanModel;
@@ -49,6 +58,27 @@

private static final String module = UrlRegexpTransform.class.getName();


private static String convertToString(Object o) {
String result = "";
if (o != null) {
if (Debug.verboseOn()) {
Debug.logVerbose("Arg Object : " + o.getClass().getName(), module);
}
if (o instanceof TemplateScalarModel) {
TemplateScalarModel s = (TemplateScalarModel) o;
try {
result = s.getAsString();
} catch (TemplateModelException e) {
Debug.logError(e, "Template Exception", module);
}
} else {
result = o.toString();
}
}
return result;
}

public boolean checkArg(Map<?, ?> args, String key, boolean defaultValue) {
if (!args.containsKey(key)) {
return defaultValue;
@@ -67,6 +97,7 @@ public Writer getWriter(final Writer out, @SuppressWarnings("rawtypes") Map args
final boolean fullPath = checkArg(args, "fullPath", false);
final boolean secure = checkArg(args, "secure", false);
final boolean encode = checkArg(args, "encode", true);
final String webSiteId = convertToString(args.get("webSiteId"));

return new Writer(out) {

@@ -103,6 +134,20 @@ public void close() throws IOException {

RequestHandler rh = RequestHandler.from(request);
out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure || request.isSecure() , encode), userLogin == null));
} else if (!webSiteId.isEmpty()) {
Delegator delegator = FreeMarkerWorker.unwrap(env.getVariable("delegator"));
if (delegator == null) {
throw new IllegalStateException("Delegator not found");
}
ComponentConfig.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);
} else if (prefix != null) {
if (prefix instanceof TemplateScalarModel) {
TemplateScalarModel s = (TemplateScalarModel) prefix;
@@ -120,7 +165,11 @@ public void close() throws IOException {
} else {
out.write(buf.toString());
}
} catch (IOException | TemplateModelException e) {
} catch (IOException |
SAXException |
TemplateModelException |
GenericEntityException |
WebAppConfigurationException e) {
throw new IOException(e.getMessage());
}
}

0 comments on commit 2f3fd5a

Please sign in to comment.
You can’t perform that action at this time.