use of com.google.common.io.ByteSource in project keywhiz by square.
the class FileAssetServletTest method loadsAsset.
@Test
public void loadsAsset() throws Exception {
File folder = tempDir.newFolder("loadsAssetTest");
File assetFile = tempDir.newFile("loadsAssetTest/asset.txt");
Files.write("loadsAssetContent", assetFile, UTF_8);
FileAssetServlet servlet = new FileAssetServlet(folder, "/ui/", "index.html");
ByteSource byteSource = servlet.loadAsset("/ui/asset.txt");
assertThat(byteSource.read()).isEqualTo(Files.toByteArray(assetFile));
}
use of com.google.common.io.ByteSource in project rhino by PLOS.
the class ArticleCrudServiceImpl method repack.
@Override
public Archive repack(ArticleIngestionIdentifier ingestionId) {
ArticleIngestion ingestion = readIngestion(ingestionId);
List<ArticleFile> files = hibernateTemplate.execute(session -> {
Query query = session.createQuery("FROM ArticleFile WHERE ingestion = :ingestion");
query.setParameter("ingestion", ingestion);
return (List<ArticleFile>) query.list();
});
Map<String, ByteSource> archiveMap = files.stream().collect(Collectors.toMap(ArticleFile::getIngestedFileName, (ArticleFile file) -> new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return contentRepoService.getRepoObject(file.getCrepoVersion());
}
}));
return Archive.pack(extractFilenameStub(ingestionId.getDoiName()) + ".zip", archiveMap);
}
use of com.google.common.io.ByteSource in project gerrit by GerritCodeReview.
the class AddSshKey method apply.
public Response<SshKeyInfo> apply(IdentifiedUser user, Input input) throws BadRequestException, IOException, ConfigInvalidException {
if (input == null) {
input = new Input();
}
if (input.raw == null) {
throw new BadRequestException("SSH public key missing");
}
final RawInput rawKey = input.raw;
String sshPublicKey = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return rawKey.getInputStream();
}
}.asCharSource(UTF_8).read();
try {
AccountSshKey sshKey = authorizedKeys.addKey(user.getAccountId(), sshPublicKey);
try {
addKeyFactory.create(user, sshKey).send();
} catch (EmailException e) {
log.error("Cannot send SSH key added message to " + user.getAccount().getPreferredEmail(), e);
}
sshKeyCache.evict(user.getUserName());
return Response.<SshKeyInfo>created(GetSshKeys.newSshKeyInfo(sshKey));
} catch (InvalidSshKeyException e) {
throw new BadRequestException(e.getMessage());
}
}
use of com.google.common.io.ByteSource in project ddf by codice.
the class CatalogFrameworkImplTest method testCreateStorageWithAttributeOverridesInvalidType.
/**
* Tests that the framework properly passes a create request to the local provider with attribute overrides.
*/
@Test
public void testCreateStorageWithAttributeOverridesInvalidType() throws Exception {
List<ContentItem> contentItems = new ArrayList<>();
Map<String, Serializable> propertiesMap = new HashMap<>();
HashMap<String, Object> attributeMap = new HashMap<>();
attributeMap.put(Metacard.CREATED, "bad date");
propertiesMap.put(Constants.ATTRIBUTE_OVERRIDES_KEY, attributeMap);
MetacardImpl newCard = new MetacardImpl();
newCard.setId(null);
MetacardType metacardType = mock(MetacardType.class);
AttributeDescriptor dateAttributeDescriptor = new AttributeDescriptorImpl(Metacard.CREATED, true, true, true, true, new AttributeType<Date>() {
private static final long serialVersionUID = 1L;
@Override
public Class<Date> getBinding() {
return Date.class;
}
@Override
public AttributeFormat getAttributeFormat() {
return AttributeFormat.DATE;
}
});
when(metacardType.getAttributeDescriptor(Metacard.TITLE)).thenReturn(dateAttributeDescriptor);
newCard.setType(metacardType);
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("blah".getBytes());
}
};
ContentItemImpl newItem = new ContentItemImpl(uuidGenerator.generateUuid(), byteSource, "application/octet-stream", "blah", 0L, newCard);
contentItems.add(newItem);
CreateResponse response = framework.create(new CreateStorageRequestImpl(contentItems, propertiesMap));
assertEquals(response.getCreatedMetacards().size(), provider.size());
assertEquals(response.getCreatedMetacards().size(), storageProvider.size());
for (Metacard curCard : response.getCreatedMetacards()) {
assertNotNull(curCard.getId());
// Assert value is not set for invalid format
assertThat(curCard.getCreatedDate(), nullValue());
}
}
use of com.google.common.io.ByteSource in project ddf by codice.
the class CatalogFrameworkImplTest method testUpdateItemWithQualifier.
/**
* Tests that the framework properly passes an update request to the local provider when the
* content item has a qualifier.
*/
@Test
public void testUpdateItemWithQualifier() throws Exception {
// store one item
MetacardImpl metacard = new MetacardImpl();
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("blah".getBytes());
}
};
ContentItemImpl contentItem = new ContentItemImpl(uuidGenerator.generateUuid(), byteSource, "application/octet-stream", "blah", 0L, metacard);
CreateResponse response = framework.create(new CreateStorageRequestImpl(Collections.singletonList(contentItem), null));
Metacard createResponseMetacard = response.getCreatedMetacards().get(0);
// update with 2 more content items that have a qualifier and the same id and metacard as the already-created item
List<ContentItem> updateRequestContentItems = new ArrayList<>();
ByteSource q1ByteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("q1 data".getBytes());
}
};
ContentItem q1ContentItem = new ContentItemImpl(createResponseMetacard.getId(), "q1", q1ByteSource, "application/octet-stream", createResponseMetacard);
updateRequestContentItems.add(q1ContentItem);
ByteSource q2ByteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return new ByteArrayInputStream("q2 data".getBytes());
}
};
ContentItem q2ContentItem = new ContentItemImpl(createResponseMetacard.getId(), "q2", q2ByteSource, "application/octet-stream", createResponseMetacard);
updateRequestContentItems.add(q2ContentItem);
UpdateStorageRequest request = new UpdateStorageRequestImpl(updateRequestContentItems, null);
List<Result> mockFederationResults = Stream.of(createResponseMetacard).map(m -> {
Result mockResult = mock(Result.class);
when(mockResult.getMetacard()).thenReturn(m);
return mockResult;
}).collect(Collectors.toList());
QueryResponseImpl queryResponse = new QueryResponseImpl(mock(QueryRequest.class), mockFederationResults, 1);
when(mockFederationStrategy.federate(anyList(), anyObject())).thenReturn(queryResponse);
// send update to framework
List<Update> returnedCards = framework.update(request).getUpdatedMetacards();
assertThat(returnedCards, hasSize(1));
final Metacard updateResponseMetacard = returnedCards.get(0).getNewMetacard();
assertThat(updateResponseMetacard.getId(), notNullValue());
assertThat(updateResponseMetacard.getResourceURI().toString(), is(contentItem.getUri()));
assertThat(updateResponseMetacard.getResourceSize(), is(Long.toString(byteSource.size())));
assertThat(response.getCreatedMetacards(), hasSize(storageProvider.size()));
// make sure that the event was posted correctly
assertThat(eventAdmin.wasEventPosted(), is(true));
}
Aggregations