use of org.talend.sdk.component.server.front.model.ErrorDictionary.PLUGIN_MISSING in project component-runtime by Talend.
the class ComponentResource method getDependency.
/**
* Return a binary of the dependency represented by `id`.
* It can be maven coordinates for dependencies or a component id.
*
* @param id the dependency identifier.
* @return the dependency binary (jar).
*/
@GET
@Path("dependency/{id}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public StreamingOutput getDependency(@PathParam("id") final String id) {
final ComponentFamilyMeta.BaseMeta<?> component = componentDao.findById(id);
final File file;
if (component != null) {
// local dep
file = componentManagerService.manager().findPlugin(component.getParent().getPlugin()).orElseThrow(() -> new WebApplicationException(Response.status(Response.Status.NOT_FOUND).type(APPLICATION_JSON_TYPE).entity(new ErrorPayload(PLUGIN_MISSING, "No plugin matching the id: " + id)).build())).getContainerFile().orElseThrow(() -> new WebApplicationException(Response.status(Response.Status.NOT_FOUND).type(APPLICATION_JSON_TYPE).entity(new ErrorPayload(PLUGIN_MISSING, "No dependency matching the id: " + id)).build()));
} else {
// just try to resolve it locally, note we would need to ensure some security here
// .map(Artifact::toPath).map(localDependencyRelativeResolver
final Artifact artifact = Artifact.from(id);
file = componentManagerService.manager().getContainer().resolve(artifact.toPath());
}
if (!file.exists()) {
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).type(APPLICATION_JSON_TYPE).entity(new ErrorPayload(PLUGIN_MISSING, "No file found for: " + id)).build());
}
return output -> {
// 5k
final byte[] buffer = new byte[40960];
try (final InputStream stream = new BufferedInputStream(new FileInputStream(file), buffer.length)) {
int count;
while ((count = stream.read(buffer)) >= 0) {
if (count == 0) {
continue;
}
output.write(buffer, 0, count);
}
}
};
}
Aggregations