Skip to content

Commit 6b825c6

Browse files
Backport: Consolidate XML parsing initialization, remove some casts (#6976) (#6984)
1 parent 630e4c1 commit 6b825c6

16 files changed

Lines changed: 293 additions & 80 deletions

File tree

api/src/org/labkey/api/cache/ehcache/EhSimpleCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.jetbrains.annotations.Nullable;
2424
import org.labkey.api.cache.CacheType;
2525
import org.labkey.api.cache.SimpleCache;
26+
import org.labkey.api.util.IntegerUtils;
2627

2728
import java.util.HashSet;
2829
import java.util.List;
@@ -51,7 +52,7 @@ public void put(@NotNull K key, V value)
5152
public void put(@NotNull K key, V value, long timeToLive)
5253
{
5354
Element element = new Element(key, value);
54-
element.setTimeToLive((int)timeToLive / 1000);
55+
element.setTimeToLive(IntegerUtils.asInteger(timeToLive / 1000)); // Convert from ms to sec
5556
_cache.put(element);
5657
}
5758

api/src/org/labkey/api/data/Aggregate.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,13 +567,13 @@ public Result getResult(ResultSet rs, Map<FieldKey, ? extends ColumnInfo> column
567567
{
568568
o = rs.getObject(aggColName);
569569
// TODO: Handle BigDecimal values
570-
if (o instanceof Number)
570+
if (o instanceof Number n)
571571
{
572-
double resultValue = ((Number)o).doubleValue();
572+
double resultValue = n.doubleValue();
573573
if (resultValue == Math.floor(resultValue))
574-
o = Long.valueOf((long) resultValue);
574+
o = n.longValue();
575575
else
576-
o = Double.valueOf(resultValue);
576+
o = resultValue;
577577
}
578578
}
579579

api/src/org/labkey/api/data/DbSchemaCache.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class DbSchemaCache
4242

4343
// Ask the DbSchemaType how long to cache each schema
4444
private final CacheTimeChooser<String> SCHEMA_CACHE_TIME_CHOOSER = (key, argument) -> {
45-
@SuppressWarnings({"unchecked"})
4645
SchemaDetails details = (SchemaDetails)argument;
4746
return details.getType().getCacheTimeToLive();
4847
};

api/src/org/labkey/api/data/DbSequence.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ public synchronized long next()
139139
}
140140

141141
/* package */
142-
synchronized long reserveSequentialBlock(int count)
142+
synchronized long reserveSequentialBlock(long count)
143143
{
144144
if (null == _lastReservedValue || _currentValue+count > _lastReservedValue)
145145
{
146-
Pair<Long, Long> reserved = DbSequenceManager.reserve(this, Math.max(count,_batchSize));
146+
Pair<Long, Long> reserved = DbSequenceManager.reserve(this, Math.max(count, _batchSize));
147147
_currentValue = reserved.first;
148148
_lastReservedValue = reserved.second;
149149
}

api/src/org/labkey/api/data/DbSequenceManager.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.sql.Connection;
3838
import java.sql.SQLException;
3939
import java.util.Collection;
40+
import java.util.Objects;
4041
import java.util.Set;
4142
import java.util.concurrent.ConcurrentHashMap;
4243

@@ -106,8 +107,8 @@ public static DbSequence getPreallocatingSequence(Container c, String name, int
106107
return _sequences.computeIfAbsent(key, (k) -> new DbSequence.Preallocate(c, name, ensure(c, name, id), batchSize));
107108
}
108109

109-
/* This is not a recommended, but if you get stuck and need to reserve a block at once */
110-
public static long reserveSequentialBlock(DbSequence seq, int count)
110+
/* This is not recommended, but if you get stuck and need to reserve a block at once */
111+
public static long reserveSequentialBlock(DbSequence seq, long count)
111112
{
112113
if (!(seq instanceof DbSequence.Preallocate))
113114
throw new IllegalStateException();
@@ -123,10 +124,7 @@ private static int ensure(Container c, String name, int id, boolean withUpdateLo
123124
{
124125
Integer rowId = getRowId(c, name, id, withUpdateLock);
125126

126-
if (null != rowId)
127-
return rowId;
128-
else
129-
return create(c, name, id, withUpdateLock);
127+
return Objects.requireNonNullElseGet(rowId, () -> create(c, name, id, withUpdateLock));
130128
}
131129

132130
public static @Nullable Integer getRowId(Container c, String name, int id)
@@ -284,7 +282,7 @@ static long next(DbSequence sequence)
284282
// .first value returned is 'current', call to next() should return current+1
285283
// .second value is reserved for use by caller
286284
// in other words, next() should return values [pair.first+1, pair.second] inclusive
287-
static Pair<Long,Long> reserve(DbSequence sequence, int count)
285+
static Pair<Long,Long> reserve(DbSequence sequence, long count)
288286
{
289287
TableInfo tinfo = getTableInfo();
290288

api/src/org/labkey/api/pipeline/file/AbstractFileAnalysisProtocol.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
import org.labkey.api.pipeline.PipelineProtocol;
2929
import org.labkey.api.util.FileType;
3030
import org.labkey.api.util.FileUtil;
31+
import org.labkey.api.util.UnexpectedException;
32+
import org.labkey.api.util.XmlBeansUtil;
3133
import org.labkey.api.view.ViewBackgroundInfo;
3234
import org.labkey.api.writer.PrintWriters;
3335
import org.xml.sax.InputSource;
3436

3537
import javax.xml.parsers.DocumentBuilder;
36-
import javax.xml.parsers.DocumentBuilderFactory;
3738
import javax.xml.transform.OutputKeys;
3839
import javax.xml.transform.Transformer;
3940
import javax.xml.transform.TransformerFactory;
@@ -105,7 +106,7 @@ public void setXml(String xml)
105106
String line;
106107
while ((line = reader.readLine()) != null)
107108
stripped.append(line.trim());
108-
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
109+
DocumentBuilder db = XmlBeansUtil.DOCUMENT_BUILDER_FACTORY.newDocumentBuilder();
109110
DOMSource xmlInput = new DOMSource(db.parse(new InputSource(new StringReader(stripped.toString()))));
110111
StreamResult xmlOutput = new StreamResult(new StringWriter());
111112
Transformer transformer = TransformerFactory.newInstance().newTransformer();
@@ -254,7 +255,15 @@ protected void save(Path file, Map<String, String> addParams, Map<String, String
254255
protected ParamParser parse()
255256
{
256257
ParamParser parser = getFactory().createParamParser();
257-
parser.parse(new ReaderInputStream(new StringReader(xml), Charset.defaultCharset()));
258+
try
259+
{
260+
parser.parse(new ReaderInputStream.Builder().setReader(new StringReader(xml)).setCharset(Charset.defaultCharset()).get());
261+
}
262+
catch (IOException e)
263+
{
264+
// Shouldn't happen since we already had the content in-memory as a String
265+
throw UnexpectedException.wrap(e);
266+
}
258267
return parser;
259268
}
260269

@@ -277,7 +286,7 @@ public FileType findInputType(Path file)
277286
public abstract List<FileType> getInputTypes();
278287

279288
@Override
280-
public abstract AbstractFileAnalysisProtocolFactory getFactory();
289+
public abstract AbstractFileAnalysisProtocolFactory<?> getFactory();
281290

282291
public abstract JOB createPipelineJob(ViewBackgroundInfo info,
283292
PipeRoot root, List<Path> filesInput,

api/src/org/labkey/api/reports/report/RedirectReport.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import org.labkey.api.data.Container;
2222
import org.labkey.api.settings.AppProps;
2323
import org.labkey.api.util.URLHelper;
24+
import org.labkey.api.util.logging.LogHelper;
2425
import org.labkey.api.view.HttpView;
2526
import org.labkey.api.view.JspView;
27+
import org.labkey.api.view.RedirectException;
2628
import org.labkey.api.view.ViewContext;
2729

2830
import java.net.MalformedURLException;
@@ -35,7 +37,7 @@
3537
*/
3638
public abstract class RedirectReport extends AbstractReport
3739
{
38-
private static final Logger LOG = LogManager.getLogger(RedirectReport.class);
40+
private static final Logger LOG = LogHelper.getLogger(RedirectReport.class, "Reports that send the user to some other URL");
3941

4042
public static final String REDIRECT_URL = ReportDescriptor.Prop.redirectUrl.name();
4143
public static final String TARGET = "target";
@@ -45,15 +47,15 @@ public RedirectReport()
4547
}
4648

4749
@Override
48-
public HttpView renderReport(ViewContext context)
50+
public HttpView<?> renderReport(ViewContext context)
4951
{
5052
String url = getUrl(context.getContainer());
5153

5254
// When rendering in a portal webpart, render the redirect link and thumbnail
5355
if (context.get(renderParam.reportWebPart.name()) != null)
5456
return new JspView<>("/org/labkey/api/reports/report/view/redirectReportWebPart.jsp", this);
5557

56-
return HttpView.redirect(url);
58+
throw new RedirectException(url);
5759
}
5860

5961
@Override

api/src/org/labkey/api/search/AbstractXMLDocumentParser.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package org.labkey.api.search;
1717

18+
import org.labkey.api.util.XmlBeansUtil;
1819
import org.xml.sax.Attributes;
1920
import org.xml.sax.ContentHandler;
2021
import org.xml.sax.SAXException;
2122
import org.xml.sax.helpers.DefaultHandler;
2223

2324
import javax.xml.parsers.ParserConfigurationException;
24-
import javax.xml.parsers.SAXParser;
25-
import javax.xml.parsers.SAXParserFactory;
25+
import java.io.BufferedInputStream;
2626
import java.io.IOException;
2727
import java.io.InputStream;
2828
import java.util.HashSet;
@@ -40,13 +40,9 @@ public abstract class AbstractXMLDocumentParser extends AbstractDocumentParser
4040
@Override
4141
public void parseContent(InputStream stream, ContentHandler handler) throws IOException, SAXException
4242
{
43-
try
43+
try (BufferedInputStream buffered = new BufferedInputStream(stream))
4444
{
45-
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
46-
parser.getXMLReader().setFeature("http://xml.org/sax/features/validation", false);
47-
parser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
48-
parser.getXMLReader().setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
49-
parser.parse(stream, createSAXHandler(handler));
45+
XmlBeansUtil.SAX_PARSER_FACTORY.newSAXParser().parse(buffered, createSAXHandler(handler));
5046
}
5147
catch (ParserConfigurationException e)
5248
{

api/src/org/labkey/api/util/HttpsUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void disableValidation(HttpsURLConnection sslConnection)
6262

6363

6464
/** Attempts a connection to the testURL, returning null on success and a Pair with error message and (possibly null) response code on failure */
65-
public static @Nullable Pair<String, Integer> testSslUrl(URL testURL, String advice)
65+
public static @Nullable Pair<String, Integer> testHttpsUrl(URL testURL, String advice)
6666
{
6767
try
6868
{
@@ -152,7 +152,7 @@ public static void checkSslRedirectConfiguration(HttpServletRequest request, int
152152

153153
// Now switch to a URL, since that's what testSslUrl() requires
154154
URL testURL = new URL(helper.getURIString());
155-
Pair<String, Integer> sslResult = HttpsUtil.testSslUrl(testURL,
155+
Pair<String, Integer> sslResult = HttpsUtil.testHttpsUrl(testURL,
156156
"This LabKey Server instance is configured to require secure connections on port " + redirectPort + ", but it does not appear to be responding " +
157157
"to HTTPS requests at " + testURL + ". Please see " + new HelpTopic("stagingServerTips").getHelpTopicHref() + " for " +
158158
"details about how to turn off the SSL redirect settings in the database.");

0 commit comments

Comments
 (0)