Skip to content

Commit

Permalink
Fixed: Merge UrlRegexpTransform and OfbizUrlTransform classes
Browse files Browse the repository at this point in the history
(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 2f3fd5a
Showing 1 changed file with 50 additions and 1 deletion.
Expand Up @@ -20,19 +20,28 @@


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


import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; 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.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.entity.GenericValue;
import org.apache.ofbiz.product.category.SeoConfigUtil; 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.RequestHandler;
import org.apache.ofbiz.webapp.control.WebAppConfigurationException;
import org.apache.oro.text.regex.Pattern; import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.Perl5Matcher; import org.apache.oro.text.regex.Perl5Matcher;
import org.xml.sax.SAXException;


import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.ext.beans.BeanModel; import freemarker.ext.beans.BeanModel;
Expand All @@ -49,6 +58,27 @@ public class UrlRegexpTransform implements TemplateTransformModel {


private static final String module = UrlRegexpTransform.class.getName(); 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) { public boolean checkArg(Map<?, ?> args, String key, boolean defaultValue) {
if (!args.containsKey(key)) { if (!args.containsKey(key)) {
return defaultValue; return defaultValue;
Expand All @@ -67,6 +97,7 @@ public Writer getWriter(final Writer out, @SuppressWarnings("rawtypes") Map args
final boolean fullPath = checkArg(args, "fullPath", false); final boolean fullPath = checkArg(args, "fullPath", false);
final boolean secure = checkArg(args, "secure", false); final boolean secure = checkArg(args, "secure", false);
final boolean encode = checkArg(args, "encode", true); final boolean encode = checkArg(args, "encode", true);
final String webSiteId = convertToString(args.get("webSiteId"));


return new Writer(out) { return new Writer(out) {


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


RequestHandler rh = RequestHandler.from(request); RequestHandler rh = RequestHandler.from(request);
out.write(seoUrl(rh.makeLink(request, response, buf.toString(), fullPath, secure || request.isSecure() , encode), userLogin == null)); 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) { } else if (prefix != null) {
if (prefix instanceof TemplateScalarModel) { if (prefix instanceof TemplateScalarModel) {
TemplateScalarModel s = (TemplateScalarModel) prefix; TemplateScalarModel s = (TemplateScalarModel) prefix;
Expand All @@ -120,7 +165,11 @@ public void close() throws IOException {
} else { } else {
out.write(buf.toString()); out.write(buf.toString());
} }
} catch (IOException | TemplateModelException e) { } catch (IOException |
SAXException |
TemplateModelException |
GenericEntityException |
WebAppConfigurationException e) {
throw new IOException(e.getMessage()); throw new IOException(e.getMessage());
} }
} }
Expand Down

0 comments on commit 2f3fd5a

Please sign in to comment.