Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/src/org/labkey/api/ApiModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
import org.labkey.api.view.ViewContext;
import org.labkey.api.view.ViewServlet;
import org.labkey.api.view.WebPartFactory;
import org.labkey.api.view.template.PageConfig;
import org.labkey.api.webdav.WebdavResolverImpl;
import org.labkey.api.wiki.WikiRendererType;
import org.labkey.api.writer.ContainerUser;
Expand Down Expand Up @@ -496,6 +497,7 @@ public void registerServlets(ServletContext servletCtx)
MultiValuedRenderContext.TestCase.class,
NameGenerator.TestCase.class,
NumberUtilsLabKey.TestCase.class,
PageConfig.TestCase.class,
PageFlowUtil.TestCase.class,
Pair.TestCase.class,
Path.TestCase.class,
Expand Down
12 changes: 3 additions & 9 deletions api/src/org/labkey/api/action/ReturnUrlForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.util.HtmlString;
import org.labkey.api.util.PageFlowUtil;
Expand All @@ -30,13 +28,9 @@
/**
* Simple form bean that includes a returnUrl property, typically used to send the user back to the page where they initiated the action.
* Often subclassed to add usage-specific additional properties.
* User: adam
* Date: Nov 22, 2007
*/
public class ReturnUrlForm
{
public static final Logger LOG = LogManager.getLogger(ReturnUrlForm.class);

private ReturnURLString _returnUrl;
private ReturnURLString _cancelUrl;
private ReturnURLString _successUrl;
Expand All @@ -53,7 +47,7 @@ public static HtmlString generateHiddenFormField(URLHelper returnUrl)

/**
* Should not typically be used, this getter/setter pair is for Spring parameter binding.
* Use {@link .getReturnActionURL()} instead.
* Use {@link getReturnActionURL()} instead.
*/
@Nullable
public String getReturnUrl()
Expand Down Expand Up @@ -124,7 +118,7 @@ public void setCancelUrl(ReturnURLString cancelUrl)

/**
* Not typically used, this getter/setter pair is for Spring parameter binding.
* Use {@link .getCancelActionURL()} instead.
* Use {@link getCancelActionURL()} instead.
*/
public ReturnURLString getCancelUrl()
{
Expand Down Expand Up @@ -162,7 +156,7 @@ public void setSuccessUrl(ReturnURLString successUrl)

/**
* Not typically used, this getter/setter pair is for Spring parameter binding.
* Use {@link .getSuccessActionURL()} instead.
* Use {@link getSuccessActionURL()} instead.
*/
public ReturnURLString getSuccessUrl()
{
Expand Down
7 changes: 4 additions & 3 deletions api/src/org/labkey/api/analytics/AnalyticsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.labkey.api.analytics;

import org.labkey.api.services.ServiceRegistry;
import org.labkey.api.util.HtmlString;
import org.labkey.api.view.HttpView;
import org.labkey.api.view.ViewContext;

Expand All @@ -35,16 +36,16 @@ static void setInstance(AnalyticsService impl)
ServiceRegistry.get().registerService(AnalyticsService.class, impl);
}

static String getTrackingScript()
static HtmlString getTrackingScript()
{
AnalyticsService svc = get();
if (svc == null)
{
return "";
return HtmlString.EMPTY_STRING;
}
return svc.getTrackingScript(HttpView.getRootContext());
}

String getTrackingScript(ViewContext viewContext);
HtmlString getTrackingScript(ViewContext viewContext);
// String getSanitizedUrl(ViewContext viewContext);
}
62 changes: 52 additions & 10 deletions api/src/org/labkey/api/collections/LabKeyCollectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -94,7 +95,7 @@ private static <K, V> void merge(Map<K, V> map, K key, V value, BiFunction<? sup
return toMapNullSafe(
keyMapper,
valueMapper,
(u, v) -> {
(u, _) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
},
mapFactory);
Expand Down Expand Up @@ -226,19 +227,48 @@ public static Collector<Object, JSONArray, JSONArray> toJSONArray()
/**
* Returns a {@link Collector} that joins {@link HtmlString}s into a single {@link HtmlString} separated by delimiter
*/
public static Collector<HtmlString, HtmlStringBuilder, HtmlString> joining(HtmlString delimiter) {
public static Collector<HtmlString, ?, HtmlString> joining(HtmlString delimiter) {
return Collector.of(
HtmlStringBuilder::of,
(builder, hs) -> {
if (!builder.isEmpty())
builder.append(delimiter);
builder.append(hs);
},
(h1, h2) -> { h1.append(h2.getHtmlString()); return h1; },
HtmlStringBuilder::getHtmlString
() -> new HtmlJoiner(delimiter),
HtmlJoiner::add,
HtmlJoiner::merge,
joiner -> joiner._builder.getHtmlString()
);
}

private static class HtmlJoiner
{
private final HtmlString _delimiter;
private final HtmlStringBuilder _builder = HtmlStringBuilder.of();
// Tracks whether any element was added rather than checking builder length, so empty elements still get delimiters
private boolean _hasElements = false;

private HtmlJoiner(HtmlString delimiter)
{
_delimiter = delimiter;
}

private void add(HtmlString hs)
{
if (_hasElements)
_builder.append(_delimiter);
_builder.append(hs);
_hasElements = true;
}

private HtmlJoiner merge(HtmlJoiner other)
{
if (other._hasElements)
{
if (_hasElements)
_builder.append(_delimiter);
_builder.append(other._builder);
_hasElements = true;
}
return this;
}
}

/**
Comment thread
labkey-adam marked this conversation as resolved.
Resolved
* Returns a {@link Collector} that joins {@link SQLFragment}s into a single {@link SQLFragment} separated by delimiter
*/
Expand Down Expand Up @@ -279,6 +309,18 @@ public void test()
assertEquals(10, mmap2.keySet().size());
}

@Test
public void testHtmlJoining()
{
assertEquals("", Stream.<HtmlString>empty().collect(joining(HtmlString.BR)).toString());
assertEquals("&lt;a&gt;", Stream.of("<a>").map(HtmlString::of).collect(joining(HtmlString.BR)).toString());
assertEquals("<br/>a<br/><br/>b<br/>", Stream.of("", "a", "", "b", "").map(HtmlString::of).collect(joining(HtmlString.BR)).toString());
assertEquals(HtmlString.join(List.of(HtmlString.EMPTY_STRING, HtmlString.of("x")), HtmlString.BR), Stream.of(HtmlString.EMPTY_STRING, HtmlString.of("x")).collect(joining(HtmlString.BR)));

List<String> values = IntStream.range(0, 1000).mapToObj(i -> i % 7 == 0 ? "" : String.valueOf(i)).toList();
assertEquals(String.join(",", values), values.parallelStream().map(HtmlString::of).collect(joining(HtmlString.of(","))).toString());
}

@Test
public void testNullSafe()
{
Expand Down
6 changes: 3 additions & 3 deletions api/src/org/labkey/api/data/parameterForm.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@
<%
Collection<QueryService.ParameterDecl> decls = bean.params;
Map values = bean.values;
String COMMA = "";
boolean first = true;
for (QueryService.ParameterDecl p : decls)
{
Object value = null==values?null:values.get(p.getName());
if (null == value)
value = p.getDefault();
%><%=unsafe(COMMA)%><%
if (!first) { %>,<% }
%>{name:<%=q(p.getName())%><%
%>,jdbctype:<%=q(p.getJdbcType().name())%><%
%>,jsontype:<%=q(p.getJdbcType().json)%><%
%>,xtype:<%=q(p.getJdbcType().xtype)%><%
%>,defaultValue:<%=q(ConvertUtils.convert(p.getDefault()))%><%
%>,value:<%=q(ConvertUtils.convert(value))%><%
%>}<%
COMMA = ",";
first = false;
}
%>];

Expand Down
6 changes: 3 additions & 3 deletions api/src/org/labkey/api/files/view/filesWebPart.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
%>
<span class="labkey-error">
The cloud config for this web part is invalid. It may not exist or may have been configured incorrectly.<br>
<%=unsafe("Contact your administrator to address this problem.")%>
Contact your administrator to address this problem.
</span>
<%
}
Expand All @@ -66,7 +66,7 @@
%>
<span class="labkey-error">
The file root for this folder is invalid. It may not exist or may have been configured incorrectly.<br>
<%=unsafe(c.hasPermission(getUser(), AdminPermission.class) ? "File roots can be configured from the <a href=\"" + projConfig + "\">project settings</a> view." : "Contact your administrator to address this problem.")%>
<% if (c.hasPermission(getUser(), AdminPermission.class)) { %>File roots can be configured from the <a href="<%=h(projConfig)%>">project settings</a> view.<% } else { %>Contact your administrator to address this problem.<% } %>
</span>
<%
}
Expand All @@ -77,7 +77,7 @@
<div id="<%=h(bean.getContentId())%>"></div>
<script type="text/javascript" nonce="<%=getScriptNonce()%>">
Ext4.onReady(function() {
var buttonActions = [<% String sep = ""; for(FilesWebPart.FilesForm.actions action : bean.getButtonConfig()) {%><%=unsafe(sep)%><%=q(action.name())%><% sep = ","; }%>];
var buttonActions = [<% boolean first = true; for (FilesWebPart.FilesForm.actions action : bean.getButtonConfig()) { if (!first) { %>,<% } %><%=q(action.name())%><% first = false; } %>];
var startDirectory;

<% if (bean.getDirectory() != null) { %>
Expand Down
14 changes: 2 additions & 12 deletions api/src/org/labkey/api/jsp/JspBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.labkey.api.jsp;

import com.google.errorprone.annotations.CompileTimeConstant;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
Expand Down Expand Up @@ -163,22 +164,11 @@ public HtmlString getExt3Image(String shortPath)
* @param s Valid, properly encoded HTML
* @return An HtmlString that wraps the parameter without encoding
*/
public HtmlString unsafe(String s)
public HtmlString unsafe(@CompileTimeConstant String s)
{
return HtmlString.unsafe(s);
}

/**
* Convenience method for asserting that a CharSequence contains valid, properly encoded HTML.
*
* @param cs Valid, properly encoded HTML
* @return An HtmlString that wraps the parameter without encoding
*/
public HtmlString unsafe(CharSequence cs)
{
return HtmlString.unsafe(cs.toString());
}

/**
* Html escape a string.
* The name comes from Embedded Ruby.
Expand Down
17 changes: 13 additions & 4 deletions api/src/org/labkey/api/query/ExportScriptModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.labkey.api.query;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Strings;
Expand All @@ -31,12 +32,12 @@
import org.labkey.api.settings.LookAndFeelProperties;
import org.labkey.api.util.DateUtil;
import org.labkey.api.util.logging.LogHelper;
import org.labkey.api.view.JspView;
import org.labkey.api.view.HttpView;
import org.labkey.api.view.NotFoundException;
import org.labkey.api.view.WebPartView;
import org.labkey.api.view.template.PageConfig;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -215,8 +216,16 @@ public static ModelAndView getExportScriptView(QueryView queryView, String scrip
if (factory == null)
throw new NotFoundException("Export script type not found: " + scriptType);

WebPartView<?> scriptView = new JspView<>("/org/labkey/api/query/createExportScript.jsp", factory.getModel(queryView));
scriptView.setFrame(WebPartView.FrameType.NOT_HTML);
ExportScriptModel model = factory.getModel(queryView);
HttpView<?> scriptView = new HttpView<>()
{
@Override
protected void renderInternal(Object m, HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/plain");
response.getWriter().write(model.getExportText());
}
};

QueryService.get().addAuditEvent(queryView, "Exported to script type " + scriptType, null);
return scriptView;
Expand Down
21 changes: 0 additions & 21 deletions api/src/org/labkey/api/query/createExportScript.jsp

This file was deleted.

19 changes: 9 additions & 10 deletions api/src/org/labkey/api/query/excelExportOptions.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
String xlsGUID = "xls_" + guid;
String iqyGUID = "iqy_" + guid;
String headerGUID = "header_" + guid;
String headerType = "xls_header_type";
String exportSelectedId = "exportSelected_";
String exportButtonId = "export_" + guid;
String signButtonId = "sign_" + guid;
Expand Down Expand Up @@ -79,7 +78,7 @@
<% } %>
<tr>
<td colspan="2"><label>Column headers:<%=helpPopup("Column Header Options", HtmlString.unsafe(sb.toString()))%></label>
<select id="<%=unsafe(headerGUID)%>" name="<%=unsafe(headerType)%>">
<select id="<%=h(headerGUID)%>" name="xls_header_type">
<labkey:options value="<%=model.getHeaderType()%>" map="<%=headerMap%>" />
</select>
</td>
Expand Down Expand Up @@ -108,14 +107,14 @@
<script type="text/javascript" nonce="<%=getScriptNonce()%>">
(function($) {
LABKEY.DataRegion.registerPane(<%=q(model.getDataRegionName())%>, function(dr) {
var xlsExportEl = $("#<%=h(xlsGUID)%>");
var xlsxExportEl = $("#<%=h(xlsxGUID)%>");
var iqyExportEl = $("#<%=h(iqyGUID)%>");
var headerEl = $("#<%=h(headerGUID)%>");
var xlsExportEl = $(<%=q("#" + xlsGUID)%>);
var xlsxExportEl = $(<%=q("#" + xlsxGUID)%>);
var iqyExportEl = $(<%=q("#" + iqyGUID)%>);
var headerEl = $(<%=q("#" + headerGUID)%>);
var includeSignButton = <%=model.isIncludeSignButton()%>;

var exportSelectedEl = $("#<%=h(exportSelectedId)%>");
var exportSelectedLabelEl = $("#<%=h(exportSelectedId + "_label")%>");
var exportSelectedEl = $(<%=q("#" + exportSelectedId)%>);
var exportSelectedLabelEl = $(<%=q("#" + exportSelectedId + "_label")%>);

var doExcelExport = function(isSign) {
var exportUrl, exportParams, exportRegionName = <%=q(exportRegionName)%>;
Expand Down Expand Up @@ -292,14 +291,14 @@
}
};

var exportButtonEl = $("#<%=h(exportButtonId)%>");
var exportButtonEl = $(<%=q("#" + exportButtonId)%>);
exportButtonEl.click(function() {
doExcelExport(false);
});

var signButtonEl;
if (includeSignButton) {
signButtonEl = $("#<%=h(signButtonId)%>");
signButtonEl = $(<%=q("#" + signButtonId)%>);
signButtonEl.click(function() {
doExcelExport(true);
});
Expand Down
Loading