@Test public void testDeleteArtifact() throws Exception { String artifactFileName = "s-ramp-press-release.pdf"; InputStream pdf = this.getClass().getResourceAsStream("/sample-files/core/" + artifactFileName); Document document = new Document(); document.setName(artifactFileName); document.setArtifactType(BaseArtifactEnum.DOCUMENT); // Add an artifact BaseArtifactType artifact = persistenceManager.persistArtifact(document, pdf); Assert.assertNotNull(artifact); Assert.assertEquals(Document.class, artifact.getClass()); Assert.assertEquals(new Long(18873l), ((DocumentArtifactType) artifact).getContentSize()); log.info( "persisted s-ramp-press-release.pdf to JCR, returned artifact uuid=" + artifact.getUuid()); // Now delete that artifact ArtifactType at = ArtifactType.valueOf(artifact); persistenceManager.deleteArtifact(document.getUuid(), at); // Now make sure we can't load it back up BaseArtifactType deleted = persistenceManager.getArtifact(document.getUuid(), at); Assert.assertNull(deleted); SrampQuery query = queryManager.createQuery("/s-ramp[@uuid = ?]"); query.setString(document.getUuid()); ArtifactSet artifactSet = query.executeQuery(); Assert.assertEquals(0, artifactSet.size()); }
/** * Gets the artifact type from the resource. * * @param gavInfo * @param archiveType */ private ArtifactType getArtifactType(MavenGavInfo gavInfo, String archiveType) { String customAT = getParamFromRepositoryUrl("artifactType"); // $NON-NLS-1$ if (gavInfo.getType().equals("pom")) { // $NON-NLS-1$ return ArtifactType.valueOf("MavenPom"); // $NON-NLS-1$ } else if (isPrimaryArtifact(gavInfo) && customAT != null) { return ArtifactType.valueOf(customAT); } else if (isPrimaryArtifact(gavInfo) && archiveType != null) { return ArtifactType.valueOf("ext", archiveType, true); // $NON-NLS-1$ } else if ("jar".equals(gavInfo.getType())) { // $NON-NLS-1$ return ArtifactType.valueOf(JavaModel.TYPE_ARCHIVE); } else if ("xml".equals(gavInfo.getType())) { // $NON-NLS-1$ return ArtifactType.XmlDocument(); } else { return ArtifactType.valueOf(ArtifactTypeEnum.Document.name()); } }
/** @see org.overlord.sramp.shell.api.shell.ShellCommand#execute() */ @Override public boolean execute() throws Exception { String filePathArg = this.requiredArgument( 0, Messages.i18n.format("Upload.InvalidArgMsg.LocalFile")); // $NON-NLS-1$ String artifactTypeArg = this.optionalArgument(1); QName clientVarName = new QName("s-ramp", "client"); // $NON-NLS-1$ //$NON-NLS-2$ SrampAtomApiClient client = (SrampAtomApiClient) getContext().getVariable(clientVarName); if (client == null) { print(Messages.i18n.format("MissingSRAMPConnection")); // $NON-NLS-1$ return false; } InputStream content = null; ZipToSrampArchive expander = null; SrampArchive archive = null; try { File file = new File(filePathArg); ArtifactType artifactType = null; if (artifactTypeArg != null) { artifactType = ArtifactType.valueOf(artifactTypeArg); if (artifactType.isExtendedType()) { artifactType = ArtifactType.ExtendedDocument(artifactType.getExtendedType()); } } else { artifactType = determineArtifactType(file); } content = FileUtils.openInputStream(file); BaseArtifactType artifact = client.uploadArtifact(artifactType, content, file.getName()); IOUtils.closeQuietly(content); // Now also add "expanded" content to the s-ramp repository expander = ZipToSrampArchiveRegistry.createExpander(artifactType, file); if (expander != null) { expander.setContextParam(DefaultMetaDataFactory.PARENT_UUID, artifact.getUuid()); archive = expander.createSrampArchive(); client.uploadBatch(archive); } // Put the artifact in the session as the active artifact QName artifactVarName = new QName("s-ramp", "artifact"); // $NON-NLS-1$ //$NON-NLS-2$ getContext().setVariable(artifactVarName, artifact); print(Messages.i18n.format("Upload.Success")); // $NON-NLS-1$ PrintArtifactMetaDataVisitor visitor = new PrintArtifactMetaDataVisitor(); ArtifactVisitorHelper.visitArtifact(visitor, artifact); } catch (Exception e) { print(Messages.i18n.format("Upload.Failure")); // $NON-NLS-1$ print("\t" + e.getMessage()); // $NON-NLS-1$ IOUtils.closeQuietly(content); return false; } return true; }
/** * Finds an existing artifact in the s-ramp repository using 'universal' form. This allows any * artifact in the s-ramp repository to be referenced as a Maven dependency using the model.type * and UUID of the artifact. * * @param client * @param artifactType * @param gavInfo * @return an existing s-ramp artifact (if found) or null (if not found) * @throws SrampClientException * @throws SrampAtomException * @throws JAXBException */ private BaseArtifactType findExistingArtifactByUniversal( SrampAtomApiClient client, MavenGavInfo gavInfo) throws SrampAtomException, SrampClientException, JAXBException { String artifactType = gavInfo.getGroupId().substring(gavInfo.getGroupId().indexOf('.') + 1); String uuid = gavInfo.getArtifactId(); try { return client.getArtifactMetaData(ArtifactType.valueOf(artifactType), uuid); } catch (Throwable t) { debug(t.getMessage()); } return null; }
/* * (non-Javadoc) * * @see * org.jboss.arquillian.container.sramp.SrampService#deployArchive(java. * lang.String, java.lang.String, java.io.InputStream) */ public BaseArtifactType deployArchive( String archiveId, String archiveName, String artifactTypeArg, InputStream content) { assert content != null; ZipToSrampArchive expander = null; SrampArchive archive = null; BaseArtifactType artifact = null; File tempResourceFile = null; try { // internal integrity check artifactCounter = client.query("/s-ramp").getTotalResults(); // First, stash the content in a temp file - we may need it multiple // times. tempResourceFile = stashResourceContent(content); content = FileUtils.openInputStream(tempResourceFile); ArtifactType artifactType = ArtifactType.valueOf(artifactTypeArg); if (artifactType.isExtendedType()) { artifactType = ArtifactType.ExtendedDocument(artifactType.getExtendedType()); } artifact = client.uploadArtifact(artifactType, content, archiveName); IOUtils.closeQuietly(content); // for all uploaded files add custom property SrampModelUtils.setCustomProperty(artifact, "arquillian-archive-id", archiveId); client.updateArtifactMetaData(artifact); content = FileUtils.openInputStream(tempResourceFile); // Now also add "expanded" content to the s-ramp repository expander = ZipToSrampArchiveRegistry.createExpander(artifactType, content); if (expander != null) { expander.setContextParam(DefaultMetaDataFactory.PARENT_UUID, artifact.getUuid()); archive = expander.createSrampArchive(); client.uploadBatch(archive); } } catch (Exception e) { log.error("Upload failure:", e); IOUtils.closeQuietly(content); } finally { SrampArchive.closeQuietly(archive); ZipToSrampArchive.closeQuietly(expander); FileUtils.deleteQuietly(tempResourceFile); } return artifact; }
/** @see org.overlord.sramp.common.shell.ShellCommand#execute() */ @Override public void execute() throws Exception { QName clientVarName = new QName("s-ramp", "client"); QName artifactVarName = new QName("s-ramp", "artifact"); QName feedVarName = new QName("s-ramp", "feed"); SrampAtomApiClient client = (SrampAtomApiClient) getContext().getVariable(clientVarName); if (client == null) { print("No S-RAMP repository connection is currently open."); return; } BaseArtifactType artifact = null; String artifactIdArg = this.optionalArgument(0); if (artifactIdArg == null) { artifact = (BaseArtifactType) getContext().getVariable(artifactVarName); if (artifact == null) { print("No active S-RAMP artifact exists. Use s-ramp:getMetaData."); return; } } else { String idType = artifactIdArg.substring(0, artifactIdArg.indexOf(':')); if ("feed".equals(idType)) { QueryResultSet rset = (QueryResultSet) getContext().getVariable(feedVarName); int feedIdx = Integer.parseInt(artifactIdArg.substring(artifactIdArg.indexOf(':') + 1)) - 1; if (feedIdx < 0 || feedIdx >= rset.size()) { throw new InvalidCommandArgumentException(0, "Feed index out of range."); } ArtifactSummary summary = rset.get(feedIdx); String artifactUUID = summary.getUuid(); artifact = client.getArtifactMetaData(summary.getType(), artifactUUID); } else if ("uuid".equals(idType)) { throw new InvalidCommandArgumentException( 0, "uuid: style artifact identifiers not yet implemented."); } else { throw new InvalidCommandArgumentException(0, "Invalid artifact id format."); } } try { client.deleteArtifact(artifact.getUuid(), ArtifactType.valueOf(artifact)); print("Successfully deleted artifact '%1$s'.", artifact.getName()); } catch (Exception e) { print("FAILED to delete the artifact."); print("\t" + e.getMessage()); } }
/** * Uploads a single deployment to S-RAMP. * * @param deploymentType * @param fileName * @param client * @param tempFile * @param responseParams * @param version * @throws Exception */ private void uploadSingleDeployment( String deploymentType, String fileName, File tempFile, Map<String, String> responseParams, String version) throws Exception { ArtifactType at = ArtifactType.valueOf(deploymentType); if (at.isExtendedType()) { at = ArtifactType.ExtendedDocument(at.getExtendedType()); } String uuid = null; // First, upload the deployment InputStream contentStream = null; try { contentStream = FileUtils.openInputStream(tempFile); BaseArtifactType artifact = at.newArtifactInstance(); artifact.setName(fileName); artifact.setVersion(version); artifact = clientAccessor.getClient().uploadArtifact(artifact, contentStream); responseParams.put("model", at.getArtifactType().getModel()); // $NON-NLS-1$ responseParams.put("type", at.getArtifactType().getType()); // $NON-NLS-1$ responseParams.put("uuid", artifact.getUuid()); // $NON-NLS-1$ uuid = artifact.getUuid(); } finally { IOUtils.closeQuietly(contentStream); } // Try to expand the artifact (works if an expander is available for the given artifact type). ZipToSrampArchive j2sramp = null; SrampArchive archive = null; try { j2sramp = ZipToSrampArchiveRegistry.createExpander(at, tempFile); if (j2sramp != null) { j2sramp.setContextParam(DefaultMetaDataFactory.PARENT_UUID, uuid); archive = j2sramp.createSrampArchive(); clientAccessor.getClient().uploadBatch(archive); } } finally { SrampArchive.closeQuietly(archive); ZipToSrampArchive.closeQuietly(j2sramp); } }
/** * * Gets the artifact content from the s-ramp repository and stores it in the {@link InputData} * object for use by Maven. * * @param gavInfo * @param inputData * @throws TransferFailedException * @throws ResourceDoesNotExistException * @throws AuthorizationException */ private void doGetArtifact(MavenGavInfo gavInfo, InputData inputData) throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException { // RESTEasy uses the current thread's context classloader to load its logger class. This // fails in Maven because the context classloader is the wagon plugin's classloader, which // doesn't know about any of the RESTEasy JARs. So here we're temporarily setting the // context classloader to the s-ramp wagon extension's classloader, which should have access // to all the right stuff. ClassLoader oldCtxCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(SrampWagon.class.getClassLoader()); try { // Query the artifact meta data using GAV info BaseArtifactType artifact = findExistingArtifact(client, gavInfo); if (artifact == null) throw new ResourceDoesNotExistException( Messages.i18n.format("ARTIFACT_NOT_FOUND", gavInfo.getName())); // $NON-NLS-1$ this.archive.addEntry(gavInfo.getFullName(), artifact, null); ArtifactType type = ArtifactType.valueOf(artifact); // Get the artifact content as an input stream InputStream artifactContent = client.getArtifactContent(type, artifact.getUuid()); inputData.setInputStream(artifactContent); } catch (ResourceDoesNotExistException e) { throw e; } catch (SrampClientException e) { if (e.getCause() instanceof HttpHostConnectException) { this.debug(Messages.i18n.format("SRAMP_CONNECTION_FAILED", e.getMessage())); // $NON-NLS-1$ } else { this.error(e.getMessage(), e); } throw new ResourceDoesNotExistException( Messages.i18n.format( "FAILED_TO_GET_RESOURCE_FROM_SRAMP", gavInfo.getName())); // $NON-NLS-1$ } catch (Throwable t) { this.error(t.getMessage(), t); } finally { Thread.currentThread().setContextClassLoader(oldCtxCL); } }
private String uploadArtifact(MavenMetaData metadata, InputStream content) throws MavenRepositoryException { String uuid = null; if (content == null) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.no.content")); // $NON-NLS-1$ } String fileName = metadata.getFileName(); PersistenceManager persistenceManager = PersistenceFactory.newInstance(); // We need to query firstly to check if there is an existing item. If // there is an existing item, then it would be updated // Adding the criterias and parameters List<String> criteria = new ArrayList<String>(); List<Object> parameters = new ArrayList<Object>(); criteria.add("@maven.artifactId = ?"); // $NON-NLS-1$ criteria.add("@maven.groupId = ?"); // $NON-NLS-1$ criteria.add("@maven.type = ?"); // $NON-NLS-1$ parameters.add(metadata.getArtifactId()); parameters.add(metadata.getGroupId()); if (StringUtils.isNotBlank(metadata.getParentType())) { parameters.add(metadata.getParentType()); } else { parameters.add(metadata.getType()); } if (StringUtils.isNotBlank(metadata.getVersion())) { criteria.add("@maven.version = ?"); // $NON-NLS-1$ parameters.add(metadata.getVersion()); } else { criteria.add("xp2:not(@maven.version)"); // $NON-NLS-1$ } if (StringUtils.isNotBlank(metadata.getParentFileName())) { criteria.add("@name = ?"); // $NON-NLS-1$ parameters.add(metadata.getParentFileName()); } if (StringUtils.isNotBlank(metadata.getSnapshotId())) { criteria.add("@maven.snapshot.id = ?"); // $NON-NLS-1$ parameters.add(metadata.getSnapshotId()); } else { criteria.add("xp2:not(@maven.snapshot.id)"); // $NON-NLS-1$ } ArtifactSet artifactSet = null; BaseArtifactType baseArtifact = null; try { // Query the previous criterias artifactSet = query(criteria, parameters); if (artifactSet.size() >= 1) { // Found some content! baseArtifact = artifactSet.iterator().next(); } } catch (SrampAtomException e) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.query.error", metadata.toString()), e); //$NON-NLS-1$ } finally { if (artifactSet != null) { artifactSet.close(); } } if (MavenFileExtensionEnum.value(metadata.getType()) != null) { if (baseArtifact != null) { boolean update = false; ArtifactType artifactType = ArtifactType.valueOf(baseArtifact.getArtifactType()); if (metadata.getType().equals(MavenFileExtensionEnum.HASH_MD5.getExtension()) || metadata.getType().equals(MavenFileExtensionEnum.HASH_SHA1.getExtension())) { String content_value = ""; // $NON-NLS-1$ try { content_value = IOUtils.toString(content); } catch (IOException e1) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.error", metadata.toString()), e1); //$NON-NLS-1$ } if (StringUtils.isNotBlank(content_value)) { if (metadata.getType().equals(MavenFileExtensionEnum.HASH_MD5.getExtension())) { SrampModelUtils.setCustomProperty( baseArtifact, JavaModel.PROP_MAVEN_HASH_MD5, content_value); update = true; } else if (metadata.getType().equals(MavenFileExtensionEnum.HASH_SHA1.getExtension())) { SrampModelUtils.setCustomProperty( baseArtifact, JavaModel.PROP_MAVEN_HASH_SHA1, content_value); update = true; } if (update) { try { persistenceManager.updateArtifact(baseArtifact, artifactType); } catch (SrampException e) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.error", metadata.toString()), e); //$NON-NLS-1$ } } } } else { try { persistenceManager.updateArtifactContent(baseArtifact.getUuid(), artifactType, content); } catch (SrampException e) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.error", metadata.toString()), e); //$NON-NLS-1$ } } } } else { BaseArtifactType persisted = null; // If there is an existing artifact in s-ramp it would be updaded // with the new content if (baseArtifact != null) { if (metadata.isSnapshotVersion() || metadata.getFileName().equals("maven-metadata.xml")) { // $NON-NLS-1$ ArtifactType artifactType = ArtifactType.valueOf(baseArtifact); try { persistenceManager.updateArtifactContent(baseArtifact.getUuid(), artifactType, content); } catch (SrampException e) { throw new MavenRepositoryException( Messages.i18n.format( "maven.resource.upload.sramp.update.content.error", //$NON-NLS-1$ baseArtifact.getUuid()), e); } persisted = baseArtifact; } else { throw new MavenRepositoryException( Messages.i18n.format( "maven.resource.upload.sramp.release.artifact.exist", //$NON-NLS-1$ metadata.getFullName())); } } else { // we need to create a new artifact in s-ramp and persist the // content ArtifactType artifactType = determineArtifactType(fileName); BaseArtifactType baseArtifactType = artifactType.newArtifactInstance(); try { persisted = persistenceManager.persistArtifact(baseArtifactType, content); } catch (SrampException e1) { throw new MavenRepositoryException( Messages.i18n.format("maven.resource.upload.sramp.new.content.error"), e1); //$NON-NLS-1$ } } // Store the metadata to the persisted artifact SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_GROUP_ID, metadata.getGroupId()); SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_ARTIFACT_ID, metadata.getArtifactId()); SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_VERSION, metadata.getVersion()); if (StringUtils.isNotBlank(metadata.getClassifier())) { SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_CLASSIFIER, metadata.getClassifier()); } if (StringUtils.isNotBlank(metadata.getType())) { SrampModelUtils.setCustomProperty(persisted, JavaModel.PROP_MAVEN_TYPE, metadata.getType()); } if (StringUtils.isNotBlank(metadata.getSnapshotId())) { SrampModelUtils.setCustomProperty( persisted, JavaModel.PROP_MAVEN_SNAPSHOT_ID, metadata.getSnapshotId()); } try { // Persist the content size, because it will be required when // reading persisted .getOtherAttributes() .put(SrampConstants.SRAMP_CONTENT_SIZE_QNAME, content.available() + ""); // $NON-NLS-1$ } catch (IOException e) { logger.error(""); // $NON-NLS-1$ } persisted.setName(metadata.getFileName()); ArtifactType artifactType = ArtifactType.valueOf(persisted); try { persistenceManager.updateArtifact(persisted, artifactType); } catch (SrampException e) { throw new MavenRepositoryException( Messages.i18n.format( "maven.resource.upload.sramp.update.content.metadata.error", //$NON-NLS-1$ persisted.getUuid()), e); } uuid = persisted.getUuid(); } return uuid; }
private MavenArtifactWrapper getArtifactContent(MavenMetaData metadata) throws MavenRepositoryException { // List of criterias and the parameters associated List<String> criteria = new ArrayList<String>(); List<Object> parameters = new ArrayList<Object>(); criteria.add("@maven.artifactId = ?"); // $NON-NLS-1$ criteria.add("@maven.groupId = ?"); // $NON-NLS-1$ criteria.add("@maven.type = ?"); // $NON-NLS-1$ parameters.add(metadata.getArtifactId()); parameters.add(metadata.getGroupId()); // If there is a parent type (in case of sha1 or md5, it is passed as // parameter if (StringUtils.isNotBlank(metadata.getParentType())) { parameters.add(metadata.getParentType()); } else { parameters.add(metadata.getType()); } // Not always it is passed the maven version. This is the case when it // is requested a file (normally maven-metadata.xml) that is stored in // the artifact subfolder if (StringUtils.isNotBlank(metadata.getVersion())) { criteria.add("@maven.version = ?"); // $NON-NLS-1$ parameters.add(metadata.getVersion()); } // If it is included a classfier it is added as parameter. if (StringUtils.isNotBlank(metadata.getClassifier())) { criteria.add("@maven.classifier = ?"); // $NON-NLS-1$ parameters.add(metadata.getClassifier()); } else { criteria.add("xp2:not(@maven.classifier)"); // $NON-NLS-1$ } if (StringUtils.isNotBlank(metadata.getSnapshotId())) { criteria.add("@maven.snapshot.id = ?"); // $NON-NLS-1$ parameters.add(metadata.getSnapshotId()); } else { criteria.add("xp2:not(@maven.snapshot.id)"); // $NON-NLS-1$ } ArtifactSet artifactSet = null; BaseArtifactType baseArtifact = null; try { // query based on the previous criterias artifactSet = query(criteria, parameters); if (artifactSet.size() >= 1) { // Found some content! baseArtifact = artifactSet.iterator().next(); } } catch (SrampAtomException e) { throw new MavenRepositoryException(Messages.i18n.format(""), e); // $NON-NLS-1$ } finally { if (artifactSet != null) { artifactSet.close(); } } // If the artifact returned is not null, then the content will be // retrieved if (baseArtifact != null) { PersistenceManager persistenceManager = PersistenceFactory.newInstance(); final InputStream artifactContent; ArtifactType artifactType = ArtifactType.valueOf(baseArtifact.getArtifactType()); Date lastModifiedDate = null; if (baseArtifact.getLastModifiedTimestamp() != null) { lastModifiedDate = baseArtifact.getLastModifiedTimestamp().toGregorianCalendar().getTime(); } int contentLength = -1; MavenFileExtensionEnum ext = MavenFileExtensionEnum.value(metadata.getType()); if (ext != null && StringUtils.isNotBlank(ext.getCustomProperty())) { // we need to set the input stream with the value of the custom // property String content = SrampModelUtils.getCustomProperty(baseArtifact, ext.getCustomProperty()); if (StringUtils.isNotBlank(content)) { artifactContent = new ByteArrayInputStream(content.getBytes()); contentLength = content.length(); } else { logger.info( Messages.i18n.format( "maven.resource.get.subcontent.not.found", baseArtifact.getUuid(), ext.getCustomProperty())); // $NON-NLS-1$ return null; } } else { // we need to set the input stream with the artifact content try { artifactContent = persistenceManager.getArtifactContent(baseArtifact.getUuid(), artifactType); } catch (SrampException e) { logger.error( Messages.i18n.format("maven.resource.get.content.error", baseArtifact.getUuid()), e); //$NON-NLS-1$ throw new MavenRepositoryException( Messages.i18n.format( "maven.resource.get.content.error", //$NON-NLS-1$ baseArtifact.getUuid()), e); } String contentSize = baseArtifact.getOtherAttributes().get(SrampConstants.SRAMP_CONTENT_SIZE_QNAME); if (StringUtils.isNotBlank(contentSize)) { contentLength = Integer.parseInt(contentSize); } } MavenArtifactWrapper wrapper = new MavenArtifactWrapper( artifactContent, contentLength, lastModifiedDate, metadata.getFileName(), artifactType.getMimeType()); return wrapper; } else { logger.error( Messages.i18n.format("maven.resource.item.null", metadata.toString())); // $NON-NLS-1$ // Return null so that the servlet can return a 404 return null; } }