use of javax.ws.rs.core.StreamingOutput in project winery by eclipse.
the class RestUtils method getCsarOfSelectedResource.
/**
* @param options the set of options that are applicable for exporting a csar
*/
public static Response getCsarOfSelectedResource(final AbstractComponentInstanceResource resource, CsarExportOptions options) {
long start = System.currentTimeMillis();
final CsarExporter exporter = new CsarExporter(RepositoryFactory.getRepository());
Map<String, Object> exportConfiguration = new HashMap<>();
StreamingOutput so = output -> {
try {
// check which options are chosen
if (options.isAddToProvenance()) {
// We wait for the accountability layer to confirm the transaction
String result = exporter.writeCsarAndSaveManifestInProvenanceLayer(resource.getId(), output).get();
LOGGER.debug("Stored state in accountability layer in transaction " + result);
} else if (options.isIncludeDependencies() && resource.getId() instanceof ServiceTemplateId) {
SelfContainmentPackager packager = new SelfContainmentPackager(RepositoryFactory.getRepository());
DefinitionsChildId selfContainedVersion = packager.createSelfContainedVersion(resource.getId());
exporter.writeSelfContainedCsar(RepositoryFactory.getRepository(), selfContainedVersion, output, exportConfiguration);
} else {
exporter.writeCsar(resource.getId(), output, exportConfiguration);
}
long duration = (System.currentTimeMillis() - start) / 1000;
LOGGER.debug("CSAR export lasted {} min {} s", (int) duration / 60, duration % 60);
} catch (Exception e) {
LOGGER.error("Error while exporting CSAR", e);
throw new WebApplicationException(e);
}
};
String contentDisposition = String.format("attachment;filename=\"%s%s\"", resource.getXmlId().getEncoded(), Constants.SUFFIX_CSAR);
return Response.ok().header("Content-Disposition", contentDisposition).type(MimeTypes.MIMETYPE_ZIP).entity(so).build();
}
use of javax.ws.rs.core.StreamingOutput in project winery by eclipse.
the class RestUtils method getDefinitionsOfSelectedResource.
/**
* Returns the plain XML for the selected resource
*/
public static Response getDefinitionsOfSelectedResource(final AbstractComponentInstanceResource resource, final URI uri) {
final ToscaExportUtil exporter = new ToscaExportUtil();
StreamingOutput so = output -> {
Map<String, Object> conf = new HashMap<>();
conf.put(CsarExportConfiguration.REPOSITORY_URI.toString(), uri);
try {
exporter.writeTOSCA(RepositoryFactory.getRepository(), resource.getId(), conf, output);
} catch (Exception e) {
throw new WebApplicationException(e);
}
output.close();
};
/*
* this code is for offering a download action // Browser offers save as
* // .tosca is more or less needed for debugging, only a CSAR makes
* sense. // Therefore, we want to have the xml opened in the browser.
* StringBuilder sb = new StringBuilder();
* sb.append("attachment;filename=\"");
* sb.append(resource.getXmlId().getEncoded()); sb.append(" - ");
* sb.append(resource.getNamespace().getEncoded()); sb.append(".xml");
* sb.append("\""); return Response.ok().header("Content-Disposition",
* sb
* .toString()).type(MediaType.APPLICATION_XML_TYPE).entity(so).buildProvenanceSmartContract();
*/
return Response.ok().type(MediaType.APPLICATION_XML).entity(so).build();
}
use of javax.ws.rs.core.StreamingOutput in project carbon-apimgt by wso2.
the class RetrieveApiArtifactsApiServiceImpl method retrieveApiArtifactsPost.
public Response retrieveApiArtifactsPost(String xWSO2Tenant, String gatewayLabel, String type, UUIDListDTO uuidList, MessageContext messageContext) throws APIManagementException {
xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
RuntimeArtifactDto runtimeArtifactDto = APIArtifactGeneratorUtil.generateAPIArtifact(uuidList.getUuids(), "", "", gatewayLabel, type, xWSO2Tenant);
if (runtimeArtifactDto != null) {
if (runtimeArtifactDto.isFile()) {
File artifact = (File) runtimeArtifactDto.getArtifact();
StreamingOutput streamingOutput = (outputStream) -> {
try {
Files.copy(artifact.toPath(), outputStream);
} finally {
Files.delete(artifact.toPath());
}
};
return Response.ok(streamingOutput).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=apis.zip").header(RestApiConstants.HEADER_CONTENT_TYPE, APIConstants.APPLICATION_ZIP).build();
} else {
SynapseArtifactListDTO synapseArtifactListDTO = new SynapseArtifactListDTO();
if (runtimeArtifactDto.getArtifact() instanceof List) {
synapseArtifactListDTO.setList((List<String>) runtimeArtifactDto.getArtifact());
synapseArtifactListDTO.setCount(((List<String>) runtimeArtifactDto.getArtifact()).size());
}
return Response.ok().entity(synapseArtifactListDTO).header(RestApiConstants.HEADER_CONTENT_TYPE, RestApiConstants.APPLICATION_JSON).build();
}
} else {
return Response.status(Response.Status.NOT_FOUND).entity(RestApiUtil.getErrorDTO(ExceptionCodes.NO_API_ARTIFACT_FOUND)).build();
}
}
use of javax.ws.rs.core.StreamingOutput in project carbon-apimgt by wso2.
the class RuntimeMetadataApiServiceImpl method runtimeMetadataGet.
public Response runtimeMetadataGet(String xWSO2Tenant, String apiId, String gatewayLabel, MessageContext messageContext) throws APIManagementException {
xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
RuntimeArtifactDto runtimeArtifactDto = RuntimeArtifactGeneratorUtil.generateMetadataArtifact(xWSO2Tenant, apiId, gatewayLabel);
if (runtimeArtifactDto != null) {
File artifact = (File) runtimeArtifactDto.getArtifact();
StreamingOutput streamingOutput = (outputStream) -> {
try {
Files.copy(artifact.toPath(), outputStream);
} finally {
Files.delete(artifact.toPath());
}
};
return Response.ok(streamingOutput).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=deployment.json").header(RestApiConstants.HEADER_CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity(RestApiUtil.getErrorDTO(ExceptionCodes.NO_API_ARTIFACT_FOUND)).build();
}
}
use of javax.ws.rs.core.StreamingOutput in project cxf by apache.
the class BinaryDataProviderTest method testReadFrom.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testReadFrom() throws Exception {
MessageBodyReader p = new BinaryDataProvider();
byte[] bytes = (byte[]) p.readFrom(byte[].class, byte[].class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream("hi".getBytes()));
assertArrayEquals(new String("hi").getBytes(), bytes);
InputStream is = (InputStream) p.readFrom(InputStream.class, InputStream.class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream("hi".getBytes()));
bytes = IOUtils.readBytesFromStream(is);
assertArrayEquals(new String("hi").getBytes(), bytes);
Reader r = (Reader) p.readFrom(Reader.class, Reader.class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream("hi".getBytes()));
assertEquals(IOUtils.toString(r), "hi");
StreamingOutput so = (StreamingOutput) p.readFrom(StreamingOutput.class, StreamingOutput.class, new Annotation[] {}, MediaType.APPLICATION_OCTET_STREAM_TYPE, new MetadataMap<String, Object>(), new ByteArrayInputStream("hi".getBytes()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
so.write(baos);
bytes = baos.toByteArray();
assertArrayEquals(new String("hi").getBytes(), bytes);
}
Aggregations