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
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ private static ActionURL getPageURL(ViewContext ctx)
{
// This is set to the outer page URL in the case of rendering a dynamic webpart; use it instead of
// the getWebPart URL.
String returnUrl = (String)ctx.get(ActionURL.Param.returnUrl.name());
String returnUrl = ctx.getString(ActionURL.Param.returnUrl.name());

if (null != returnUrl)
{
Expand Down
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 @@ -200,6 +200,7 @@
import org.labkey.api.view.LabKeyKaptchaServlet;
import org.labkey.api.view.Portal;
import org.labkey.api.view.RedirectorServlet;
import org.labkey.api.view.ViewContext;
import org.labkey.api.view.ViewServlet;
import org.labkey.api.view.WebPartFactory;
import org.labkey.api.webdav.WebdavResolverImpl;
Expand Down Expand Up @@ -507,6 +508,7 @@ public void registerServlets(ServletContext servletCtx)
URIUtil.TestCase.class,
ValidEmail.TestCase.class,
VersionNumber.TestCase.class,
ViewContext.TestCase.class,
XmlBeansUtil.TestCase.class
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ protected Object runScript(ScriptEngine engine, ViewContext context, List<ParamR
// An empty session id is allowed; we just
// don't do any session sharing in this case
//
String reportSessionId = (String) context.get(renderParam.reportSessionId.name());
String reportSessionId = context.getString(renderParam.reportSessionId.name());

if (!StringUtils.isEmpty(reportSessionId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public Thumbnail cleanup(ScriptEngineReport report, ContainerUser context)

private static <K> K handleParameters(ScriptEngineReport report, Collection<ParamReplacement> parameters, ParameterHandler<K> handler) throws IOException
{
String sections = (String) HttpView.currentContext().get(renderParam.showSection.name());
String sections = HttpView.currentContext().getString(renderParam.showSection.name());
List<String> sectionNames = Collections.emptyList();

if (sections != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public Thumbnail cleanup(ScriptProcessReport report, ContainerUser context)

private static <K> K handleParameters(ScriptProcessReport report, Collection<ParamReplacement> parameters, ParameterHandler<K> handler) throws IOException
{
String sections = (String)HttpView.currentContext().get(renderParam.showSection.name());
String sections = HttpView.currentContext().getString(renderParam.showSection.name());
List<String> sectionNames = Collections.emptyList();

if (sections != null)
Expand Down
6 changes: 3 additions & 3 deletions api/src/org/labkey/api/reports/report/ScriptReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public Results _generateResults(ViewContext context, boolean allowAsyncQuery) th

if (!StringUtils.isEmpty(filterParam))
{
final String filterValue = (String)context.get(filterParam);
final String filterValue = context.getString(filterParam);

if (filterValue != null)
{
Expand Down Expand Up @@ -365,12 +365,12 @@ public ActionURL getEditReportURL(ViewContext context)
@Override
public HttpView<?> getRunReportView(ViewContext context) throws Exception
{
String tabId = (String) context.get("tabId");
String tabId = context.getString("tabId");

if (null == tabId)
tabId = context.getActionURL().getParameter("tabId");

String webpartString = (String) context.get(Report.renderParam.reportWebPart.name());
String webpartString = context.getString(Report.renderParam.reportWebPart.name());
boolean webpart = (null != webpartString && BooleanFormat.getInstance().parseObject(webpartString));

// Module-based reports are always read-only, but we still allow viewing the report source in the source tab.
Expand Down
43 changes: 41 additions & 2 deletions api/src/org/labkey/api/view/ViewContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import jakarta.servlet.http.HttpSession;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Test;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.security.HasPermission;
Expand All @@ -42,6 +44,7 @@
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.mock.web.MockHttpServletRequest;

import java.io.Closeable;
import java.util.ArrayList;
Expand All @@ -56,8 +59,6 @@
/**
* Holds many of the key pieces of context about an HTTP request, including the {@link HttpServletRequest} and
* {@link HttpServletResponse} objects, the {@link User} making the request, etc.
* User: matthewb
* Date: Mar 20, 2005
*/
public class ViewContext implements MessageSource, ContainerContext, ContainerUser, ApplicationContextAware, HasPermission, HasHttpRequest
{
Expand Down Expand Up @@ -213,11 +214,31 @@ public Map<String, Object> getExtendedProperties()
return _map;
}

public Object get(String key)
{
return _map.get(key);
}

@Deprecated // Left behind so not every module needs to be recompiled immediately. TODO: Remove
public Object get(Object key)
{
return _map.get(key);
}

/*
* Safer and more convenient than using get() with a String cast. Returns _map.get(key) if it's null or a String.
* Otherwise, throws BadRequestException. See GH Issue 1631.
*/
public @Nullable String getString(String key)
{
Object value = _map.get(key);
if (value == null)
return null;
if (value instanceof String stringValue)
return stringValue;
throw new BadRequestException(String.format("Expected a single string value for key: %s", key)); // No logging, no mothership
}

public Object put(String key, Object value)
{
return _map.put(key, value);
Expand Down Expand Up @@ -524,4 +545,22 @@ public boolean isRobot()
var r = getRequest();
return null != r && PageFlowUtil.isRobotUserAgent(r.getHeader("User-Agent"));
}

public static class TestCase extends Assert
{
@Test
public void testGetString()
{
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("single", "value");
request.addParameter("repeated", "one", "two");
ViewContext context = new ViewContext(request, null, null);
context.put("number", 42);

assertEquals("value", context.getString("single"));
assertNull(context.getString("missing"));
assertThrows(BadRequestException.class, () -> context.getString("repeated"));
assertThrows(BadRequestException.class, () -> context.getString("number"));
}
}
}
12 changes: 6 additions & 6 deletions query/src/org/labkey/query/reports/ReportsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1318,10 +1318,10 @@ public static class StreamFileAction extends SimpleViewAction<Object>
@Override
public ModelAndView getView(Object o, BindException errors) throws Exception
{
String sessionKey = (String) getViewContext().get(ImageUtil.FILE_SESSION_PARAM);
String deleteFile = (String) getViewContext().get(ImageUtil.DELETE_FILE_PARAM);
String attachment = (String) getViewContext().get(ImageUtil.ATTACHMENT_PARAM);
String cacheFile = (String) getViewContext().get(ImageUtil.CACHE_FILE_PARAM);
String sessionKey = getViewContext().getString(ImageUtil.FILE_SESSION_PARAM);
String deleteFile = getViewContext().getString(ImageUtil.DELETE_FILE_PARAM);
String attachment = getViewContext().getString(ImageUtil.ATTACHMENT_PARAM);
String cacheFile = getViewContext().getString(ImageUtil.CACHE_FILE_PARAM);
if (sessionKey != null)
{
FileLike file = ImageUtil.getFileFromSession(getViewContext().getRequest(), sessionKey);
Expand Down Expand Up @@ -2234,8 +2234,8 @@ public static class ReportSectionsAction extends ReadOnlyApiAction<Object>
public ApiResponse execute(Object o, BindException errors)
{
ApiSimpleResponse response = new ApiSimpleResponse();
ReportIdentifier reportId = ReportService.get().getReportIdentifier((String)getViewContext().get(ReportDescriptor.Prop.reportId.name()), getViewContext().getUser(), getViewContext().getContainer());
String sections = (String)getViewContext().get(Report.renderParam.showSection.name());
ReportIdentifier reportId = ReportService.get().getReportIdentifier(getViewContext().getString(ReportDescriptor.Prop.reportId.name()), getViewContext().getUser(), getViewContext().getContainer());
String sections = getViewContext().getString(Report.renderParam.showSection.name());
if (reportId != null)
{
Report report = reportId.getReport(getViewContext());
Expand Down
14 changes: 7 additions & 7 deletions study/src/org/labkey/study/controllers/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ protected Report getReport(QueryReportForm form)
{
if (_report == null)
{
String reportId = (String)getViewContext().get(DATASET_REPORT_ID_PARAMETER_NAME);
String reportId = getViewContext().getString(DATASET_REPORT_ID_PARAMETER_NAME);

ReportIdentifier identifier = ReportService.get().getReportIdentifier(reportId, getViewContext().getUser(), getViewContext().getContainer());
if (identifier != null)
Expand All @@ -838,7 +838,7 @@ protected ModelAndView getHtmlView(QueryReportForm form, BindException errors) t
return HttpView.redirect(createRedirectURLfrom(DatasetAction.class, context));
}

int datasetId = NumberUtils.toInt((String)context.get(Dataset.DATASET_KEY), -1);
int datasetId = NumberUtils.toInt(context.getString(Dataset.DATASET_KEY), -1);
Dataset def = StudyManager.getInstance().getDatasetDefinition(getStudyRedirectIfNull(), datasetId);

if (def != null)
Expand Down Expand Up @@ -896,7 +896,7 @@ private DatasetDefinition getDatasetDefinition()
}
else
{
String entityId = (String)getViewContext().get("entityId");
String entityId = getViewContext().getString("entityId");
if (null != entityId)
_def = StudyManager.getInstance().getDatasetDefinitionByEntityId(getStudyRedirectIfNull(), entityId);
}
Expand Down Expand Up @@ -1151,7 +1151,7 @@ public ModelAndView getView(ParticipantForm form, BindException errors)
if (!errorMsg.isEmpty())
return HtmlView.err(errorMsg.toString());

String viewName = (String) getViewContext().get(DATASET_VIEW_NAME_PARAMETER_NAME);
String viewName = getViewContext().getString(DATASET_VIEW_NAME_PARAMETER_NAME);

CohortFilter cohortFilter = CohortFilterFactory.getFromURL(getContainer(), getUser(), getViewContext().getActionURL(), DatasetQueryView.DATAREGION);
// display the next and previous buttons only if we have a cached participant index
Expand Down Expand Up @@ -2090,7 +2090,7 @@ public ModelAndView getView(VisitForm form, boolean reshow, BindException errors

redirectToSharedVisitStudy(study, getViewContext().getActionURL());

int id = NumberUtils.toInt((String)getViewContext().get("id"));
int id = NumberUtils.toInt(getViewContext().getString("id"));
_v = StudyManager.getInstance().getVisitForRowId(study, id);
if (_v == null)
{
Expand All @@ -2113,7 +2113,7 @@ public boolean handlePost(VisitForm form, BindException errors)
redirectToSharedVisitStudy(study, getViewContext().getActionURL());

// UNDONE: how do I get struts to handle this checkbox?
postedVisit.setShowByDefault(null != StringUtils.trimToNull((String)getViewContext().get("showByDefault")));
postedVisit.setShowByDefault(null != StringUtils.trimToNull(getViewContext().getString("showByDefault")));

// UNDONE: reshow is broken for this form, but we have to validate
Collection<VisitImpl> visits = StudyManager.getInstance().getVisitManager(study).getVisits();
Expand Down Expand Up @@ -3116,7 +3116,7 @@ public void validateCommand(DeleteDatasetRowsForm target, Errors errors)
@Override
public boolean handlePost(DeleteDatasetRowsForm form, BindException errors)
{
String originalSourceLsid = (String)getViewContext().get("sourceLsid");
String originalSourceLsid = getViewContext().getString("sourceLsid");

Dataset.PublishSource publishSource = _def.getPublishSource();
if (form.getPublishSourceId() != null && publishSource != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static class StreamFileAction extends SimpleViewAction<Object>
@Override
public ModelAndView getView(Object o, BindException errors) throws Exception
{
String sessionKey = (String) getViewContext().get(ImageUtil.FILE_SESSION_PARAM);
String sessionKey = getViewContext().getString(ImageUtil.FILE_SESSION_PARAM);
if (null == sessionKey)
{
//TODO: Return a GIF that says not found??
Expand Down Expand Up @@ -539,13 +539,6 @@ public ActionURL getSuccessURL(CrosstabDesignBean crosstabDesignBean)
@Override
public void addNavTrail(NavTree root)
{
/*
ViewContext context = getViewContext();
int datasetId = null == context.get(DatasetDefinition.DATASETKEY) ? 0 : Integer.parseInt((String) context.get(DatasetDefinition.DATASETKEY));
int visitRowId = null == context.get("visitRowId") ? 0 : Integer.parseInt((String) context.get("visitRowId"));

return _appendNavTrail(root, "Crosstab View Builder", datasetId, visitRowId);
*/
setHelpTopic("crosstabReports");
root.addChild("Crosstab Report Builder");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public boolean handlePost(Object o, BindException errors)
@Override
public ActionURL getSuccessURL(Object o)
{
String redirect = (String)getViewContext().get("redirect");
String redirect = getViewContext().getString("redirect");
if (redirect != null)
return new ActionURL(redirect);

Expand Down Expand Up @@ -441,7 +441,7 @@ private MutableSecurityPolicy policyFromPost(Map<Integer, String> group2Perm, Se
@Override
public ActionURL getSuccessURL(Object o)
{
String redirect = (String) getViewContext().get("redirect");
String redirect = getViewContext().getString("redirect");
if (redirect != null)
return new ActionURL(redirect);

Expand Down Expand Up @@ -608,7 +608,7 @@ public boolean handlePost(StudySecurityForm form, BindException errors)
@Override
public ActionURL getSuccessURL(StudySecurityForm studySecurityForm)
{
String redirect = (String) getViewContext().get("redirect");
String redirect = getViewContext().getString("redirect");
if (redirect != null)
return new ActionURL(redirect);

Expand Down
2 changes: 1 addition & 1 deletion study/src/org/labkey/study/reports/ParticipantReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public HttpView renderReport(ViewContext context)

JspView<ReportsController.ParticipantReportForm> view = new JspView<>("/org/labkey/study/view/participantReport.jsp", form);

String rwp = (String)context.get("reportWebPart");
String rwp = context.getString("reportWebPart");
form.setExpanded(rwp == null);

form.setAllowOverflow(!BooleanUtils.toBoolean(rwp));
Expand Down
Loading