From 60ec8f95ce678c9701dab6aacadeac51b2625b0a Mon Sep 17 00:00:00 2001 From: Dustin Jenkins Date: Mon, 24 Jun 2024 10:30:45 -0700 Subject: [PATCH] Add comment to method, and Checkstyle re-order imports. --- .../resources/StorageItemServerResource.java | 81 ++++++++++--------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/src/main/java/net/canfar/storage/web/resources/StorageItemServerResource.java b/src/main/java/net/canfar/storage/web/resources/StorageItemServerResource.java index 54dea4c..74b45b2 100644 --- a/src/main/java/net/canfar/storage/web/resources/StorageItemServerResource.java +++ b/src/main/java/net/canfar/storage/web/resources/StorageItemServerResource.java @@ -73,19 +73,38 @@ import ca.nrc.cadc.auth.NotAuthenticatedException; import ca.nrc.cadc.net.RemoteServiceException; import ca.nrc.cadc.util.StringUtil; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import javax.security.auth.Subject; +import javax.servlet.ServletContext; import net.canfar.storage.PathUtils; -import net.canfar.storage.web.config.StorageConfiguration; -import net.canfar.storage.web.config.VOSpaceServiceConfigManager; -import org.opencadc.gms.GroupURI; -import org.opencadc.vospace.*; -import org.opencadc.vospace.client.VOSpaceClient; import net.canfar.storage.web.StorageItemFactory; +import net.canfar.storage.web.config.StorageConfiguration; import net.canfar.storage.web.config.VOSpaceServiceConfig; +import net.canfar.storage.web.config.VOSpaceServiceConfigManager; import net.canfar.storage.web.restlet.StorageApplication; import net.canfar.storage.web.view.StorageItem; - import org.json.JSONObject; +import org.opencadc.gms.GroupURI; +import org.opencadc.vospace.ContainerNode; +import org.opencadc.vospace.LinkNode; +import org.opencadc.vospace.Node; +import org.opencadc.vospace.NodeNotFoundException; +import org.opencadc.vospace.NodeProperty; +import org.opencadc.vospace.VOS; +import org.opencadc.vospace.VOSURI; +import org.opencadc.vospace.client.VOSpaceClient; import org.opencadc.vospace.client.async.RecursiveSetNode; import org.restlet.data.Status; import org.restlet.ext.json.JsonRepresentation; @@ -93,29 +112,14 @@ import org.restlet.resource.Post; import org.restlet.resource.ResourceException; -import javax.security.auth.Subject; -import javax.servlet.ServletContext; -import java.io.*; -import java.net.URI; -import java.net.URISyntaxException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.security.*; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - public class StorageItemServerResource extends SecureServerResource { // Page size for the initial page display. private static final int DEFAULT_DISPLAY_PAGE_SIZE = 35; + private final VOSpaceServiceConfigManager voSpaceServiceConfigManager; StorageItemFactory storageItemFactory; VOSpaceClient voSpaceClient; - VOSpaceServiceConfig currentService; - private final VOSpaceServiceConfigManager voSpaceServiceConfigManager; /** * Empty constructor needed for Restlet to manage it. Needs to be public. @@ -126,11 +130,12 @@ public StorageItemServerResource() { /** * Only used for testing as no Request is coming through to initialize it as it would in Production. - * @param storageConfiguration The StorageConfiguration object. - * @param voSpaceServiceConfigManager The VOSpaceServiceConfigManager object. - * @param storageItemFactory The StorageItemFactory object. - * @param voSpaceClient The VOSpaceClient object. - * @param serviceConfig The current VOSpace Service. + * + * @param storageConfiguration The StorageConfiguration object. + * @param voSpaceServiceConfigManager The VOSpaceServiceConfigManager object. + * @param storageItemFactory The StorageItemFactory object. + * @param voSpaceClient The VOSpaceClient object. + * @param serviceConfig The current VOSpace Service. */ StorageItemServerResource(StorageConfiguration storageConfiguration, VOSpaceServiceConfigManager voSpaceServiceConfigManager, @@ -159,7 +164,7 @@ public StorageItemServerResource() { /** * Set-up method. This ensures there is a context first before pulling * out some necessary objects for further work. - *

+ *

* Tester */ @Override @@ -173,12 +178,12 @@ protected void doInit() throws ResourceException { private void initializeStorageItemFactory() { final ServletContext servletContext = getServletContext(); this.storageItemFactory = new StorageItemFactory((servletContext == null) - ? StorageApplication.DEFAULT_CONTEXT_PATH - : servletContext.getContextPath(), + ? StorageApplication.DEFAULT_CONTEXT_PATH + : servletContext.getContextPath(), this.currentService); } - Path getCurrentPath() { + Path getCurrentPath() { if (getRequestAttributes().containsKey("path")) { final String pathInRequest = getRequestAttribute("path"); return Paths.get(pathInRequest); @@ -229,7 +234,7 @@ final T getCurrentNode(final VOS.Detail detail) { @SuppressWarnings("unchecked") T getNode(final Path nodePath, final VOS.Detail detail, final Integer limit) - throws ResourceException { + throws ResourceException { final Map queryPayload = new HashMap<>(); if (limit != null) { queryPayload.put("limit", limit); @@ -343,7 +348,7 @@ void setInheritedPermissions(final Path nodePath) throws Exception { * Resolve this link Node's target to its final destination. This method * will follow the target of the provided LinkNode, and continue to do so * until an external URL is found, or Node that is not a Link Node. - *

+ *

* Finally, this method will redirect to the appropriate endpoint. * * @throws Exception For any parsing errors. @@ -359,7 +364,6 @@ void resolveLink() throws Exception { * * @param linkNode The LinkNode to resolve. * @return URI of the target. - * * @throws NodeNotFoundException If the target is not found. */ private URI resolveLink(final LinkNode linkNode) throws NodeNotFoundException { @@ -481,6 +485,11 @@ String getDisplayName() throws Exception { return identityManager.toDisplayString(getVOSpaceCallingSubject()); } + /** + * Convenience method to obtain a Subject targeted for the current VOSpace backend. When using Tokens, for example, the AuthenticationToken instance + * in the Subject's Public Credentials will contain the domain of the backend VOSpace API. + * @return Subject instance. Never null. + */ Subject getVOSpaceCallingSubject() throws Exception { return super.getCallingSubject(new URL(this.voSpaceClient.getBaseURL())); } @@ -511,7 +520,7 @@ public void update(final JsonRepresentation payload) throws Exception { currentNode.getReadOnlyGroup().clear(); if (keySet.contains("readGroup") && StringUtil.hasText(jsonObject.getString("readGroup"))) { final GroupURI newReadGroupURI = - new GroupURI(storageConfiguration.getGroupURI(jsonObject.getString("readGroup"))); + new GroupURI(storageConfiguration.getGroupURI(jsonObject.getString("readGroup"))); currentNode.getReadOnlyGroup().add(newReadGroupURI); } else { currentNode.clearReadOnlyGroups = true; @@ -520,7 +529,7 @@ public void update(final JsonRepresentation payload) throws Exception { currentNode.getReadWriteGroup().clear(); if (keySet.contains("writeGroup") && StringUtil.hasText(jsonObject.getString("writeGroup"))) { final GroupURI newReadWriteGroupURI = - new GroupURI(storageConfiguration.getGroupURI(jsonObject.getString("writeGroup"))); + new GroupURI(storageConfiguration.getGroupURI(jsonObject.getString("writeGroup"))); currentNode.getReadWriteGroup().add(newReadWriteGroupURI); } else { currentNode.clearReadWriteGroups = true;