diff --git a/metadata-template-rest/src/main/java/org/irods/jargon/rest/metadatatemplate/security/WebSecurityConfig.java b/metadata-template-rest/src/main/java/org/irods/jargon/rest/metadatatemplate/security/WebSecurityConfig.java index bc7dea0..a1c7354 100644 --- a/metadata-template-rest/src/main/java/org/irods/jargon/rest/metadatatemplate/security/WebSecurityConfig.java +++ b/metadata-template-rest/src/main/java/org/irods/jargon/rest/metadatatemplate/security/WebSecurityConfig.java @@ -107,8 +107,7 @@ protected void configure(HttpSecurity http) throws Exception { .authenticationEntryPoint(irodsBasicAuthEntryPoint).and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS); - http.addFilterBefore(connectionCloseFilter, - SecurityContextPersistenceFilter.class); + http.addFilterBefore(connectionCloseFilter, SecurityContextPersistenceFilter.class); } /** diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/ApiOriginFilter.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/ApiOriginFilter.java deleted file mode 100644 index 96385cb..0000000 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/ApiOriginFilter.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.irods.jargon.rest.metadatatemplate; - -import java.io.IOException; - -import javax.servlet.*; -import javax.servlet.http.HttpServletResponse; - -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-22T16:39:27.094-05:00") -public class ApiOriginFilter implements javax.servlet.Filter { - public void doFilter(ServletRequest request, ServletResponse response, - FilterChain chain) throws IOException, ServletException { - HttpServletResponse res = (HttpServletResponse) response; - res.addHeader("Access-Control-Allow-Origin", "*"); - res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); - res.addHeader("Access-Control-Allow-Headers", "Content-Type"); - chain.doFilter(request, response); - } - - public void destroy() {} - - public void init(FilterConfig filterConfig) throws ServletException {} -} \ No newline at end of file diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/PingApi.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/PingApi.java index 91456c7..44aa18e 100644 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/PingApi.java +++ b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/PingApi.java @@ -19,7 +19,7 @@ @Path("/ping") -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-23T16:25:45.614-05:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-23T17:08:15.207-05:00") public class PingApi { private final PingApiService delegate = PingApiServiceFactory.getPingApi(); @@ -27,8 +27,8 @@ public class PingApi { @Produces({ "application/json", "application/xml" }) - public Response server( @QueryParam("midTierOnly") Boolean midTierOnly,@Context SecurityContext securityContext) + public Response ping( @QueryParam("midTierOnly") Boolean midTierOnly,@Context SecurityContext securityContext) throws NotFoundException { - return delegate.server(midTierOnly,securityContext); + return delegate.ping(midTierOnly,securityContext); } } diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/PingApiService.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/PingApiService.java index e6eae8d..1e1f7e6 100644 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/PingApiService.java +++ b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/PingApiService.java @@ -14,8 +14,8 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-23T16:25:45.614-05:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-23T17:08:15.207-05:00") public abstract class PingApiService { - public abstract Response server(Boolean midTierOnly,SecurityContext securityContext) + public abstract Response ping(Boolean midTierOnly,SecurityContext securityContext) throws NotFoundException; } diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/BuildFormApiServiceImpl.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/BuildFormApiServiceImpl.java index 75dc654..ffc809e 100644 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/BuildFormApiServiceImpl.java +++ b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/BuildFormApiServiceImpl.java @@ -17,8 +17,12 @@ import java.util.List; import org.irods.jargon.rest.metadatatemplate.NotFoundException; +import org.irods.jargon.rest.security.IrodsAuthentication; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; import java.io.FileNotFoundException; import java.io.IOException; @@ -27,41 +31,54 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; +@Component @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-22T16:39:27.094-05:00") public class BuildFormApiServiceImpl extends BuildFormApiService { - + private final Logger log = LoggerFactory.getLogger(this.getClass()); - + + @Autowired private IRODSAccessObjectFactory irodsAccessObjectFactory; - + @Override public Response buildForm(MetadataTemplateRequest body, SecurityContext securityContext) throws NotFoundException { - + JargonMetadataResolver resolver = null; MetadataTemplate template = null; String uuidString = null; + if (securityContext == null) { + // throw new IllegalArgumentException("null securityContext"); + return Response.status(405).entity("null securityContext").build(); + } + + log.info("authentication:{}", SecurityContextHolder.getContext() + .getAuthentication()); + IrodsAuthentication irodsAuthentication = (IrodsAuthentication) SecurityContextHolder + .getContext().getAuthentication(); + Form form = new Form(); - + try { - resolver = new JargonMetadataResolver(irodsAccount, + resolver = new JargonMetadataResolver( + irodsAuthentication.getIrodsAccount(), irodsAccessObjectFactory); } catch (JargonException e) { log.error( "JargonException: JargonMetadataResolver could not be created", e); } - + if (resolver == null) { log.error("Unable to instantiate JargonMetadataResolver"); - + return Response .status(500) .entity("Internal server error - Unable to instantiate JargonMetadataResolver") .build(); } - + try { if (!body.getUuid().isEmpty()) { // Prefer UUID if provided @@ -69,8 +86,10 @@ public Response buildForm(MetadataTemplateRequest body, } else if (!body.getFqName().isEmpty()) { // Prefer FQ name if no UUID provided template = resolver.findTemplateByFqName(body.getFqName()); - } else if (!body.getName().isEmpty() && !body.getActiveDir().isEmpty()) { - template = resolver.findTemplateByName(body.getName(), body.getActiveDir()); + } else if (!body.getName().isEmpty() + && !body.getActiveDir().isEmpty()) { + template = resolver.findTemplateByName(body.getName(), + body.getActiveDir()); } else { return Response .status(400) @@ -80,13 +99,10 @@ public Response buildForm(MetadataTemplateRequest body, } catch (FileNotFoundException e) { log.error("Metadata template file not found"); - return Response - .status(404) - .entity("File not found") - .build(); + return Response.status(404).entity("File not found").build(); } catch (MetadataTemplateParsingException e) { log.error("Error parsing metadata template file JSON"); - + return Response .status(500) .entity("Internal server error - Error parsing metadata template file JSON") @@ -106,7 +122,7 @@ public Response buildForm(MetadataTemplateRequest body, .entity("Internal server error - IOException when trying to load metadata template file") .build(); } - + form.setName(template.getName()); form.setDescription(template.getDescription()); form.setUniqueId(uuidString); @@ -218,17 +234,15 @@ public Response buildForm(MetadataTemplateRequest body, } } // TODO else if (other type of MetadataTemplate) - return Response - .status(200) - .entity(form) - .build(); + return Response.status(200).entity(form).build(); } - + public IRODSAccessObjectFactory getIrodsAccessObjectFactory() { return irodsAccessObjectFactory; } - - public void setIrodsObjectFactory(IRODSAccessObjectFactory irodsAccessObjectFactory) { + + public void setIrodsObjectFactory( + IRODSAccessObjectFactory irodsAccessObjectFactory) { this.irodsAccessObjectFactory = irodsAccessObjectFactory; } } diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ExecuteFieldApiServiceImpl.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ExecuteFieldApiServiceImpl.java index 98d50bb..36c8199 100644 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ExecuteFieldApiServiceImpl.java +++ b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ExecuteFieldApiServiceImpl.java @@ -1,26 +1,206 @@ package org.irods.jargon.rest.metadatatemplate.impl; +import org.irods.jargon.core.exception.JargonException; +import org.irods.jargon.core.pub.IRODSAccessObjectFactory; +import org.irods.jargon.formbot.FormBotExecutionEnum; +import org.irods.jargon.formbot.FormBotExecutionResult; +import org.irods.jargon.formbot.FormBotValidationEnum; +import org.irods.jargon.metadatatemplate.FormBasedMetadataTemplate; +import org.irods.jargon.metadatatemplate.JargonMetadataExporter; +import org.irods.jargon.metadatatemplate.JargonMetadataResolver; +import org.irods.jargon.metadatatemplate.MetadataElement; +import org.irods.jargon.metadatatemplate.MetadataTemplate; +import org.irods.jargon.metadatatemplate.MetadataTemplateParsingException; +import org.irods.jargon.metadatatemplate.MetadataTemplateProcessingException; +import org.irods.jargon.metadatatemplate.TemplateTypeEnum; +import org.irods.jargon.metadatatemplate.ValidationReturnEnum; +import org.irods.jargon.metadatatemplate.ValidatorSingleton; import org.irods.jargon.rest.metadatatemplate.*; import org.irods.jargon.rest.metadatatemplate.model.*; - -import org.irods.jargon.rest.metadatatemplate.model.Field; -import org.irods.jargon.rest.metadatatemplate.model.ExecutionResult; - import java.util.List; + import org.irods.jargon.rest.metadatatemplate.NotFoundException; +import org.irods.jargon.rest.security.IrodsAuthentication; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; +import com.fasterxml.jackson.core.JsonProcessingException; + +import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; +@Component @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-23T16:25:45.614-05:00") public class ExecuteFieldApiServiceImpl extends ExecuteFieldApiService { - @Override - public Response executeField(Field field,String path,SecurityContext securityContext) - throws NotFoundException { - // do some magic! - return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build(); - } + + private final Logger log = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private IRODSAccessObjectFactory irodsAccessObjectFactory; + + @Override + public Response executeField(Field field, String path, + SecurityContext securityContext) throws NotFoundException { + + ExecutionResult executionResult = null; + + if (securityContext == null) { + // throw new IllegalArgumentException("null securityContext"); + return Response.status(405).entity("null securityContext").build(); + } + + if (field == null || field.getName().isEmpty() || field.getUniqueId().isEmpty()) { + log.error("Insufficient information to execute field: both name and uniqueId must be populated"); + + executionResult = new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Ill-formed request - both name and uniqueId in field must be populated"); + return Response.status(400).entity(executionResult).build(); + } + + if (path == null || path.isEmpty()){ + log.error("Insufficient information to execute field: path must be populated"); + + executionResult = new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Ill-formed request - path must be populated must be populated"); + return Response.status(400).entity(executionResult).build(); + } + + JargonMetadataResolver resolver = null; + JargonMetadataExporter exporter = null; + MetadataTemplate template = null; + + log.info("authentication:{}", SecurityContextHolder.getContext() + .getAuthentication()); + IrodsAuthentication irodsAuthentication = (IrodsAuthentication) SecurityContextHolder + .getContext().getAuthentication(); + + try { + resolver = new JargonMetadataResolver( + irodsAuthentication.getIrodsAccount(), + irodsAccessObjectFactory); + } catch (JargonException e) { + log.error( + "JargonException: JargonMetadataResolver could not be created", + e); + } + + if (resolver == null) { + log.error("Unable to instantiate JargonMetadataResolver"); + + executionResult = new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Could not create JargonMetadataResolver"); + + return Response.status(500).entity(executionResult).build(); + } + + exporter = new JargonMetadataExporter(irodsAccessObjectFactory, + irodsAuthentication.getIrodsAccount()); + + try { + template = resolver.findTemplateByUUID(field.getUniqueId()); + } catch (MetadataTemplateParsingException e) { + log.error("MetadataTemplateParsingException: Error parsing metadata template"); + + executionResult = new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Error parsing metadata template"); + + return Response.status(500).entity(executionResult).build(); + } catch (FileNotFoundException e) { + log.error("FileNotFoundException: Metadata template not found"); + + executionResult = new ExecutionResult( + FormBotExecutionEnum.ERROR, "Metadata template not found"); + + return Response.status(404).entity(executionResult).build(); + } catch (MetadataTemplateProcessingException e) { + log.error("MetadataTemplateProcessingException: Error processing metadata template"); + + executionResult = new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Error processing metadata template"); + + return Response.status(500).entity(executionResult).build(); + } catch (IOException e) { + log.error("IOException: Error reading metadata template from disk"); + + executionResult = new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Error reading metadata template from disk"); + + return Response.status(500).entity(executionResult).build(); + } + + if (template.getType() == TemplateTypeEnum.FORM_BASED) { + FormBasedMetadataTemplate fbmt = (FormBasedMetadataTemplate) template; + for (MetadataElement me : fbmt.getElements()) { + + if (me.getName().equalsIgnoreCase(field.getName())) { + me.setCurrentValue(field.getCurrentValue()); + ValidationReturnEnum validationReturn = ValidatorSingleton.VALIDATOR + .validate(irodsAuthentication.getIrodsAccount(), irodsAccessObjectFactory, + me); + + if (validationReturn == ValidationReturnEnum.SUCCESS + || validationReturn == ValidationReturnEnum.NOT_VALIDATED + || validationReturn == ValidationReturnEnum.REGEX_SYNTAX_ERROR) { + try { + exporter.saveElementToSystemMetadataOnObject(me, + path); + } catch (JargonException e) { + log.error("JargonException when trying to add metadata to data object"); +// return new FormBotExecutionResult( +// FormBotExecutionEnum.ERROR, +// "JargonException when trying to add metadata to data object"); + executionResult = new ExecutionResult(FormBotExecutionEnum.ERROR, "JargonException when trying to save metadata to iRODS object"); + return Response.status(500).entity(executionResult).build(); + } + +// return new FormBotExecutionResult( +// FormBotExecutionEnum.SUCCESS, +// "Metadata added to data object"); + executionResult = new ExecutionResult(FormBotExecutionEnum.SUCCESS, "Metadata added to iRODS object"); + return Response.status(200).entity(executionResult) + .build(); + + } else { + String retString = "Validation failed for field " + + field.getName() + " with value " + field.getCurrentValue(); +// return new FormBotExecutionResult( +// FormBotExecutionEnum.VALIDATION_FAILED, +// retString); + executionResult = new ExecutionResult(FormBotExecutionEnum.VALIDATION_FAILED, retString); + return Response.status(200).entity(executionResult) + .build(); + + } + } + } + } // TODO else if (other type of MetadataTemplate) + + executionResult = new ExecutionResult(FormBotExecutionEnum.ERROR, + "Field not found"); + + return Response.status(404).entity(executionResult).build(); + } + + public IRODSAccessObjectFactory getIrodsAccessObjectFactory() { + return irodsAccessObjectFactory; + } + + public void setIrodsObjectFactory( + IRODSAccessObjectFactory irodsAccessObjectFactory) { + this.irodsAccessObjectFactory = irodsAccessObjectFactory; + } } diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ExecuteFormApiServiceImpl.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ExecuteFormApiServiceImpl.java index 351ab2b..2f2f979 100644 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ExecuteFormApiServiceImpl.java +++ b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ExecuteFormApiServiceImpl.java @@ -1,26 +1,212 @@ package org.irods.jargon.rest.metadatatemplate.impl; +import org.irods.jargon.core.exception.JargonException; +import org.irods.jargon.core.pub.IRODSAccessObjectFactory; +import org.irods.jargon.formbot.FormBotExecutionEnum; +import org.irods.jargon.formbot.FormBotExecutionResult; +import org.irods.jargon.metadatatemplate.FormBasedMetadataTemplate; +import org.irods.jargon.metadatatemplate.JargonMetadataExporter; +import org.irods.jargon.metadatatemplate.JargonMetadataResolver; +import org.irods.jargon.metadatatemplate.MetadataElement; +import org.irods.jargon.metadatatemplate.MetadataTemplate; +import org.irods.jargon.metadatatemplate.MetadataTemplateParsingException; +import org.irods.jargon.metadatatemplate.MetadataTemplateProcessingException; +import org.irods.jargon.metadatatemplate.TemplateTypeEnum; +import org.irods.jargon.metadatatemplate.ValidationReturnEnum; +import org.irods.jargon.metadatatemplate.ValidatorSingleton; import org.irods.jargon.rest.metadatatemplate.*; import org.irods.jargon.rest.metadatatemplate.model.*; - -import org.irods.jargon.rest.metadatatemplate.model.Form; -import org.irods.jargon.rest.metadatatemplate.model.ExecutionResult; - +import java.util.ArrayList; import java.util.List; + import org.irods.jargon.rest.metadatatemplate.NotFoundException; +import org.irods.jargon.rest.security.IrodsAuthentication; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; +import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; +@Component @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-23T16:25:45.614-05:00") public class ExecuteFormApiServiceImpl extends ExecuteFormApiService { - @Override - public Response executeForm(Form form,String path,SecurityContext securityContext) - throws NotFoundException { - // do some magic! - return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build(); - } + + private final Logger log = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private IRODSAccessObjectFactory irodsAccessObjectFactory; + + @Override + public Response executeForm(Form form, String path, + SecurityContext securityContext) throws NotFoundException { + List executionResultList = new ArrayList(); + + if (securityContext == null) { + // throw new IllegalArgumentException("null securityContext"); + return Response.status(405).entity("null securityContext").build(); + } + + if (form == null || form.getName().isEmpty() || form.getUniqueId().isEmpty()) { + log.error("Insufficient information to execute field: both name and uniqueId must be populated"); + + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Ill-formed request - both name and uniqueId in field must be populated")); + return Response.status(400).entity(executionResultList).build(); + } + + + if (path == null || path.isEmpty()){ + log.error("Insufficient information to execute field: path must be populated"); + + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Ill-formed request - path must be populated must be populated")); + return Response.status(400).entity(executionResultList).build(); + } + + JargonMetadataResolver resolver = null; + JargonMetadataExporter exporter = null; + MetadataTemplate template = null; + + log.info("authentication:{}", SecurityContextHolder.getContext() + .getAuthentication()); + IrodsAuthentication irodsAuthentication = (IrodsAuthentication) SecurityContextHolder + .getContext().getAuthentication(); + + try { + resolver = new JargonMetadataResolver( + irodsAuthentication.getIrodsAccount(), + irodsAccessObjectFactory); + } catch (JargonException e) { + log.error( + "JargonException: JargonMetadataResolver could not be created", + e); + } + + if (resolver == null) { + log.error("Unable to instantiate JargonMetadataResolver"); + + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Could not create JargonMetadataResolver")); + + return Response.status(500).entity(executionResultList).build(); + } + + exporter = new JargonMetadataExporter(irodsAccessObjectFactory, + irodsAuthentication.getIrodsAccount()); + + try { + template = resolver.findTemplateByUUID(form.getUniqueId()); + } catch (MetadataTemplateParsingException e) { + log.error("MetadataTemplateParsingException: Error parsing metadata template"); + + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Error parsing metadata template")); + + return Response.status(500).entity(executionResultList).build(); + } catch (FileNotFoundException e) { + log.error("FileNotFoundException: Metadata template not found"); + + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.ERROR, "Metadata template not found")); + + return Response.status(404).entity(executionResultList).build(); + } catch (MetadataTemplateProcessingException e) { + log.error("MetadataTemplateProcessingException: Error processing metadata template"); + + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Error processing metadata template")); + + return Response.status(500).entity(executionResultList).build(); + } catch (IOException e) { + log.error("IOException: Error reading metadata template from disk"); + + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.ERROR, + "Error reading metadata template from disk")); + + return Response.status(500).entity(executionResultList).build(); + } + + boolean validationFailed = false; + boolean error = false; + + if (template.getType() == TemplateTypeEnum.FORM_BASED) { + FormBasedMetadataTemplate fbmt = (FormBasedMetadataTemplate) template; + for (Field field : form.getFields()) { + for (MetadataElement me : fbmt.getElements()) { + if (me.getName().equalsIgnoreCase(field.getName())) { + me.setCurrentValue(field.getCurrentValue()); + ValidationReturnEnum validationReturn = ValidatorSingleton.VALIDATOR + .validate(irodsAuthentication.getIrodsAccount(), + irodsAccessObjectFactory, me); + if (validationReturn == ValidationReturnEnum.SUCCESS + || validationReturn == ValidationReturnEnum.NOT_VALIDATED + || validationReturn == ValidationReturnEnum.REGEX_SYNTAX_ERROR) { + try { + exporter.saveElementToSystemMetadataOnObject( + me, path); + } catch (JargonException e) { + log.error("JargonException when trying to add metadata to data object"); + executionResultList + .add(new ExecutionResult( + FormBotExecutionEnum.ERROR, + "JargonException when adding metadata to data obj")); + error = true; + break; + } + + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.SUCCESS, + validationReturn.toString())); + break; + } else { + executionResultList.add(new ExecutionResult( + FormBotExecutionEnum.VALIDATION_FAILED, + validationReturn.toString())); + validationFailed = true; + break; + } + } + } + } + } // TODO else if (other type of MetadataTemplate) + + if (error) { + executionResultList.add(0, new ExecutionResult( + FormBotExecutionEnum.ERROR, + "At least one field generated an error")); + } else if (validationFailed) { + executionResultList.add(0, new ExecutionResult( + FormBotExecutionEnum.VALIDATION_FAILED, + "At least one field failed validation")); + } else { + executionResultList.add(0, new ExecutionResult( + FormBotExecutionEnum.SUCCESS, + "Metadata added to iRODS object")); + } + + return Response.status(200).entity(executionResultList).build(); + } + + public IRODSAccessObjectFactory getIrodsAccessObjectFactory() { + return irodsAccessObjectFactory; + } + + public void setIrodsObjectFactory( + IRODSAccessObjectFactory irodsAccessObjectFactory) { + this.irodsAccessObjectFactory = irodsAccessObjectFactory; + } } diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/PingApiServiceImpl.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/PingApiServiceImpl.java index 393038c..6e4d704 100644 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/PingApiServiceImpl.java +++ b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/PingApiServiceImpl.java @@ -1,25 +1,105 @@ package org.irods.jargon.rest.metadatatemplate.impl; +import org.irods.jargon.core.exception.JargonException; +import org.irods.jargon.core.pub.EnvironmentalInfoAO; +import org.irods.jargon.core.pub.IRODSAccessObjectFactory; +import org.irods.jargon.rest.security.IrodsAuthentication; import org.irods.jargon.rest.metadatatemplate.*; import org.irods.jargon.rest.metadatatemplate.model.*; - -import org.irods.jargon.rest.metadatatemplate.model.Ping; - -import java.util.List; import org.irods.jargon.rest.metadatatemplate.NotFoundException; - -import java.io.InputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-23T16:25:45.614-05:00") +@Component +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-23T17:08:15.207-05:00") public class PingApiServiceImpl extends PingApiService { + + private final Logger log = LoggerFactory.getLogger(this.getClass()); + + @Autowired + private IRODSAccessObjectFactory irodsAccessObjectFactory; + @Override - public Response server(Boolean midTierOnly,SecurityContext securityContext) + /** + * Send back a heartbeat ping + * + * @param midTierOnly + * boolean. If true, don't ping iRODS, + * if false, iRODS is pinged and the round trip time + * is returned + * @param securityContext + * @return {@link Response} + * @throws NotFoundException + */ + public Response ping(Boolean midTierOnly,SecurityContext securityContext) throws NotFoundException { - // do some magic! - return Response.ok().entity(new ApiResponseMessage(ApiResponseMessage.OK, "magic!")).build(); + log.info("ping()"); + if (midTierOnly == null) { + midTierOnly = false; + } + + if (securityContext == null) { + // throw new IllegalArgumentException("null securityContext"); + return Response.status(405).entity("null securityContext").build(); + } + + log.info("authentication:{}", SecurityContextHolder.getContext() + .getAuthentication()); + IrodsAuthentication irodsAuthentication = (IrodsAuthentication) SecurityContextHolder + .getContext().getAuthentication(); + + float millis = 0.0f; + if (!midTierOnly) { + log.info("doing a ping!"); + log.info("authentication:{}", securityContext.getUserPrincipal()); + long startTime = System.currentTimeMillis(); + log.info("startTime:{}", startTime); + try { + log.info("accessing irods"); + EnvironmentalInfoAO environmentalInfoAO = irodsAccessObjectFactory + .getEnvironmentalInfoAO(irodsAuthentication + .getIrodsAccount()); + environmentalInfoAO.getIRODSServerCurrentTime(); + log.info("got properties"); + } catch (JargonException e) { + log.error("Error pinging back end irods server", e); + // throw new IrodsRestException(e); + return Response.status(500).entity("Error pinging back end irods server").build(); + } + long endTime = System.currentTimeMillis(); + millis = endTime - startTime; + log.info("endTime:{}", endTime); + + } + + log.info("principal:{}", securityContext.getUserPrincipal()); + + Ping ping = new Ping(); + ping.setPingTime(millis); + + return Response.status(200).entity(ping).build(); } + + /** + * @return the irodsAccessObjectFactory + */ + public IRODSAccessObjectFactory getIrodsAccessObjectFactory() { + return irodsAccessObjectFactory; + } + + /** + * @param irodsAccessObjectFactory + * the irodsAccessObjectFactory to set + */ + public void setIrodsAccessObjectFactory( + IRODSAccessObjectFactory irodsAccessObjectFactory) { + this.irodsAccessObjectFactory = irodsAccessObjectFactory; + } } diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ValidateFieldApiServiceImpl.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ValidateFieldApiServiceImpl.java index 0a8c44c..734e79f 100644 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ValidateFieldApiServiceImpl.java +++ b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ValidateFieldApiServiceImpl.java @@ -21,8 +21,12 @@ import java.util.List; import org.irods.jargon.rest.metadatatemplate.NotFoundException; +import org.irods.jargon.rest.security.IrodsAuthentication; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -35,11 +39,13 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; +@Component @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-22T16:39:27.094-05:00") public class ValidateFieldApiServiceImpl extends ValidateFieldApiService { private final Logger log = LoggerFactory.getLogger(this.getClass()); + @Autowired private IRODSAccessObjectFactory irodsAccessObjectFactory; @Override @@ -48,42 +54,46 @@ public Response validateField(Field body, SecurityContext securityContext) ValidationResult validationResult = null; + if (securityContext == null) { + // throw new IllegalArgumentException("null securityContext"); + return Response.status(405).entity("null securityContext").build(); + } + if (body.getName().isEmpty() || body.getUniqueId().isEmpty()) { log.error("Insufficient information to validate field: both name and uniqueId must be populated"); validationResult = new ValidationResult( FormBotValidationEnum.ERROR, "Ill-formed request - both name and uniqueId must be populated"); - return Response - .status(400) - .entity(validationResult) - .build(); + return Response.status(400).entity(validationResult).build(); } - JargonMetadataResolver resolver = null; MetadataTemplate template = null; - + + log.info("authentication:{}", SecurityContextHolder.getContext() + .getAuthentication()); + IrodsAuthentication irodsAuthentication = (IrodsAuthentication) SecurityContextHolder + .getContext().getAuthentication(); + try { - resolver = new JargonMetadataResolver(irodsAccount, + resolver = new JargonMetadataResolver( + irodsAuthentication.getIrodsAccount(), irodsAccessObjectFactory); } catch (JargonException e) { log.error( "JargonException: JargonMetadataResolver could not be created", e); } - + if (resolver == null) { log.error("Unable to instantiate JargonMetadataResolver"); - + validationResult = new ValidationResult( FormBotValidationEnum.ERROR, "Could not create JargonMetadataResolver"); - - return Response - .status(500) - .entity(validationResult) - .build(); + + return Response.status(500).entity(validationResult).build(); } try { @@ -94,44 +104,31 @@ public Response validateField(Field body, SecurityContext securityContext) validationResult = new ValidationResult( FormBotValidationEnum.ERROR, "Error parsing metadata template"); - - return Response - .status(500) - .entity(validationResult) - .build(); + + return Response.status(500).entity(validationResult).build(); } catch (FileNotFoundException e) { log.error("FileNotFoundException: Metadata template not found"); - + validationResult = new ValidationResult( - FormBotValidationEnum.ERROR, - "Metadata template not found"); - - return Response - .status(404) - .entity(validationResult) - .build(); + FormBotValidationEnum.ERROR, "Metadata template not found"); + + return Response.status(404).entity(validationResult).build(); } catch (MetadataTemplateProcessingException e) { log.error("MetadataTemplateProcessingException: Error processing metadata template"); validationResult = new ValidationResult( FormBotValidationEnum.ERROR, "Error processing metadata template"); - - return Response - .status(500) - .entity(validationResult) - .build(); + + return Response.status(500).entity(validationResult).build(); } catch (IOException e) { log.error("IOException: Error reading metadata template from disk"); validationResult = new ValidationResult( FormBotValidationEnum.ERROR, "Error reading metadata template from disk"); - - return Response - .status(500) - .entity(validationResult) - .build(); + + return Response.status(500).entity(validationResult).build(); } if (template.getType() == TemplateTypeEnum.FORM_BASED) { @@ -140,8 +137,8 @@ public Response validateField(Field body, SecurityContext securityContext) if (me.getName().equalsIgnoreCase(body.getName())) { me.setCurrentValue(body.getCurrentValue()); ValidationReturnEnum validationReturn = ValidatorSingleton.VALIDATOR - .validate(irodsAccount, irodsAccessObjectFactory, - me); + .validate(irodsAuthentication.getIrodsAccount(), + irodsAccessObjectFactory, me); FormBotValidationEnum fbv; if (validationReturn == ValidationReturnEnum.SUCCESS) { @@ -156,29 +153,24 @@ public Response validateField(Field body, SecurityContext securityContext) validationResult = new ValidationResult(fbv, validationReturn.toString()); - return Response - .status(200) - .entity(validationResult) + return Response.status(200).entity(validationResult) .build(); } } } // TODO else if (other type of MetadataTemplate) - validationResult = new ValidationResult( - FormBotValidationEnum.ERROR, + validationResult = new ValidationResult(FormBotValidationEnum.ERROR, "Field not found"); - - return Response - .status(404) - .entity(validationResult) - .build(); + + return Response.status(404).entity(validationResult).build(); } - + public IRODSAccessObjectFactory getIrodsAccessObjectFactory() { return irodsAccessObjectFactory; } - - public void setIrodsObjectFactory(IRODSAccessObjectFactory irodsAccessObjectFactory) { + + public void setIrodsObjectFactory( + IRODSAccessObjectFactory irodsAccessObjectFactory) { this.irodsAccessObjectFactory = irodsAccessObjectFactory; } } diff --git a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ValidateFormApiServiceImpl.java b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ValidateFormApiServiceImpl.java index ec4d8f0..c37a81f 100644 --- a/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ValidateFormApiServiceImpl.java +++ b/metadata-template-service-impl/src/main/java/org/irods/jargon/rest/metadatatemplate/impl/ValidateFormApiServiceImpl.java @@ -21,8 +21,12 @@ import java.util.List; import org.irods.jargon.rest.metadatatemplate.NotFoundException; +import org.irods.jargon.rest.security.IrodsAuthentication; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -35,11 +39,13 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; +@Component @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaResteasyServerCodegen", date = "2017-02-22T16:39:27.094-05:00") public class ValidateFormApiServiceImpl extends ValidateFormApiService { private final Logger log = LoggerFactory.getLogger(this.getClass()); + @Autowired private IRODSAccessObjectFactory irodsAccessObjectFactory; @Override @@ -48,6 +54,11 @@ public Response validateForm(Form body, SecurityContext securityContext) List returnList = new ArrayList(); + if (securityContext == null) { + // throw new IllegalArgumentException("null securityContext"); + return Response.status(405).entity("null securityContext").build(); + } + if (body.getUniqueId().isEmpty() || body.getFields().isEmpty()) { log.error("Insufficient information to validate form: both uniqueId and fields list must be populated"); @@ -60,8 +71,14 @@ public Response validateForm(Form body, SecurityContext securityContext) JargonMetadataResolver resolver = null; MetadataTemplate template = null; + log.info("authentication:{}", SecurityContextHolder.getContext() + .getAuthentication()); + IrodsAuthentication irodsAuthentication = (IrodsAuthentication) SecurityContextHolder + .getContext().getAuthentication(); + try { - resolver = new JargonMetadataResolver(irodsAccount, + resolver = new JargonMetadataResolver( + irodsAuthentication.getIrodsAccount(), irodsAccessObjectFactory); } catch (JargonException e) { log.error( @@ -119,7 +136,8 @@ public Response validateForm(Form body, SecurityContext securityContext) if (me.getName().equalsIgnoreCase(field.getName())) { me.setCurrentValue(field.getCurrentValue()); ValidationReturnEnum validationReturn = ValidatorSingleton.VALIDATOR - .validate(irodsAccount, + .validate( + irodsAuthentication.getIrodsAccount(), irodsAccessObjectFactory, me); if ((validationReturn == ValidationReturnEnum.SUCCESS) || (validationReturn == ValidationReturnEnum.NOT_VALIDATED) @@ -149,11 +167,8 @@ public Response validateForm(Form body, SecurityContext securityContext) FormBotValidationEnum.SUCCESS, "All fields passed validation")); } - - return Response - .status(200) - .entity(returnList) - .build(); + + return Response.status(200).entity(returnList).build(); } public IRODSAccessObjectFactory getIrodsAccessObjectFactory() {