Search in sources :

Example 1 with ErrorPayload

use of org.talend.sdk.component.server.front.model.error.ErrorPayload in project component-runtime by Talend.

the class DocumentationResourceTest method missingDoc.

@Test
void missingDoc() {
    final String id = client.getComponentId("chain", "list");
    final Response response = base.path("documentation/component/{id}").resolveTemplate("id", id).request(APPLICATION_JSON_TYPE).get();
    assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
    final ErrorPayload payload = response.readEntity(ErrorPayload.class);
    assertEquals(ErrorDictionary.COMPONENT_MISSING, payload.getCode());
    assertEquals("No component '" + id + "'", payload.getDescription());
}
Also used : Response(javax.ws.rs.core.Response) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Test(org.junit.jupiter.api.Test)

Example 2 with ErrorPayload

use of org.talend.sdk.component.server.front.model.error.ErrorPayload in project component-runtime by Talend.

the class ConnectionSecurityProvider method filter.

@Override
public void filter(final ContainerRequestContext requestContext) throws IOException {
    if (Boolean.TRUE.equals(request.getAttribute(SKIP))) {
        return;
    }
    final OnConnection onConnection = new OnConnection();
    onConnectionEvent.fire(onConnection);
    if (!onConnection.isValid()) {
        requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).entity(new ErrorPayload(UNAUTHORIZED, "Invalid connection credentials")).type(APPLICATION_JSON_TYPE).build());
    }
}
Also used : OnConnection(org.talend.sdk.component.server.service.security.event.OnConnection) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload)

Example 3 with ErrorPayload

use of org.talend.sdk.component.server.front.model.error.ErrorPayload in project component-runtime by Talend.

the class ComponentResource method getDetail.

/**
 * Returns the set of metadata about a few components identified by their 'id'.
 *
 * @param language the language for display names/placeholders/....
 * @param ids the component identifiers to request.
 * @return the list of details for the requested components.
 */
// TODO: max ids.length
@GET
// bulk mode to avoid to fetch components one by one when reloading a pipeline/job
@Path("details")
public ComponentDetailList getDetail(@QueryParam("language") @DefaultValue("en") final String language, @QueryParam("identifiers") final String[] ids) {
    if (ids == null || ids.length == 0) {
        return new ComponentDetailList(emptyList());
    }
    final Map<String, ErrorPayload> errors = new HashMap<>();
    final List<ComponentDetail> details = Stream.of(ids).map(id -> ofNullable(componentDao.findById(id)).orElseGet(() -> {
        errors.put(id, new ErrorPayload(COMPONENT_MISSING, "No component '" + id + "'"));
        return null;
    })).filter(Objects::nonNull).map(meta -> {
        final Optional<Container> plugin = manager.findPlugin(meta.getParent().getPlugin());
        if (!plugin.isPresent()) {
            errors.put(meta.getId(), new ErrorPayload(PLUGIN_MISSING, "No plugin '" + meta.getParent().getPlugin() + "'"));
            return null;
        }
        final Container container = plugin.get();
        final Optional<DesignModel> model = ofNullable(meta.get(DesignModel.class));
        if (!model.isPresent()) {
            errors.put(meta.getId(), new ErrorPayload(DESIGN_MODEL_MISSING, "No design model '" + meta.getId() + "'"));
            return null;
        }
        final Locale locale = localeMapper.mapLocale(language);
        final ComponentDetail componentDetail = new ComponentDetail();
        componentDetail.setLinks(emptyList());
        componentDetail.setId(createMetaId(container, meta));
        componentDetail.setVersion(meta.getVersion());
        componentDetail.setIcon(meta.getIcon());
        componentDetail.setInputFlows(model.get().getInputFlows());
        componentDetail.setOutputFlows(model.get().getOutputFlows());
        componentDetail.setType(ComponentFamilyMeta.ProcessorMeta.class.isInstance(meta) ? "processor" : "input");
        componentDetail.setDisplayName(meta.findBundle(container.getLoader(), locale).displayName().orElse(meta.getName()));
        componentDetail.setProperties(propertiesService.buildProperties(meta.getParameterMetas(), container.getLoader(), locale, null).collect(toList()));
        componentDetail.setActions(actionsService.findActions(meta.getParent().getName(), container, locale, meta));
        return componentDetail;
    }).filter(Objects::nonNull).collect(toList());
    if (!errors.isEmpty()) {
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(errors).build());
    }
    return new ComponentDetailList(details);
}
Also used : Locale(java.util.Locale) WebApplicationException(javax.ws.rs.WebApplicationException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DesignModel(org.talend.sdk.component.design.extension.DesignModel) ComponentDetailList(org.talend.sdk.component.server.front.model.ComponentDetailList) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Container(org.talend.sdk.component.container.Container) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) ComponentDetail(org.talend.sdk.component.server.front.model.ComponentDetail) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 4 with ErrorPayload

use of org.talend.sdk.component.server.front.model.error.ErrorPayload in project component-runtime by Talend.

the class ComponentResource method familyIcon.

/**
 * Returns a particular family icon in raw bytes.
 *
 * @param id the family identifier.
 * @return the family icon in binary form.
 */
@GET
@Path("icon/family/{id}")
public Response familyIcon(@PathParam("id") final String id) {
    // todo: add caching if SvgIconResolver becomes used a lot - not the case ATM
    final ComponentFamilyMeta meta = componentFamilyDao.findById(id);
    if (meta == null) {
        return Response.status(Response.Status.NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.COMPONENT_MISSING, "No family for identifier: " + id)).type(APPLICATION_JSON_TYPE).build();
    }
    final Optional<Container> plugin = manager.findPlugin(meta.getPlugin());
    if (!plugin.isPresent()) {
        return Response.status(Response.Status.NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.PLUGIN_MISSING, "No plugin '" + meta.getPlugin() + "' for identifier: " + id)).type(APPLICATION_JSON_TYPE).build();
    }
    final IconResolver.Icon iconContent = iconResolver.resolve(plugin.get().getLoader(), meta.getIcon());
    if (iconContent == null) {
        return Response.status(Response.Status.NOT_FOUND).entity(new ErrorPayload(ErrorDictionary.ICON_MISSING, "No icon for family identifier: " + id)).type(APPLICATION_JSON_TYPE).build();
    }
    return Response.ok(iconContent.getBytes()).type(iconContent.getType()).build();
}
Also used : ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Container(org.talend.sdk.component.container.Container) IconResolver(org.talend.sdk.component.server.service.IconResolver) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 5 with ErrorPayload

use of org.talend.sdk.component.server.front.model.error.ErrorPayload 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);
            }
        }
    };
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Produces(javax.ws.rs.Produces) PropertiesService(org.talend.sdk.component.server.service.PropertiesService) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) Path(javax.ws.rs.Path) Icon(org.talend.sdk.component.server.front.model.Icon) Collections.singletonList(java.util.Collections.singletonList) ComponentDetail(org.talend.sdk.component.server.front.model.ComponentDetail) ComponentManagerService(org.talend.sdk.component.server.service.ComponentManagerService) MediaType(javax.ws.rs.core.MediaType) Link(org.talend.sdk.component.server.front.model.Link) QueryParam(javax.ws.rs.QueryParam) Collectors.toMap(java.util.stream.Collectors.toMap) Consumes(javax.ws.rs.Consumes) Locale(java.util.Locale) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) ComponentId(org.talend.sdk.component.server.front.model.ComponentId) ComponentFamilyDao(org.talend.sdk.component.server.dao.ComponentFamilyDao) ContainerComponentRegistry(org.talend.sdk.component.runtime.manager.ContainerComponentRegistry) ComponentDetailList(org.talend.sdk.component.server.front.model.ComponentDetailList) Collections.emptyList(java.util.Collections.emptyList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StreamingOutput(javax.ws.rs.core.StreamingOutput) DependencyDefinition(org.talend.sdk.component.server.front.model.DependencyDefinition) ErrorDictionary(org.talend.sdk.component.server.front.model.ErrorDictionary) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) Dependencies(org.talend.sdk.component.server.front.model.Dependencies) DESIGN_MODEL_MISSING(org.talend.sdk.component.server.front.model.ErrorDictionary.DESIGN_MODEL_MISSING) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) LocaleMapper(org.talend.sdk.component.server.service.LocaleMapper) PLUGIN_MISSING(org.talend.sdk.component.server.front.model.ErrorDictionary.PLUGIN_MISSING) RequestKey(org.talend.sdk.component.server.front.base.internal.RequestKey) ActionsService(org.talend.sdk.component.server.service.ActionsService) Collections.emptyMap(java.util.Collections.emptyMap) POST(javax.ws.rs.POST) Container(org.talend.sdk.component.container.Container) Optional.ofNullable(java.util.Optional.ofNullable) Artifact(org.talend.sdk.component.dependencies.maven.Artifact) COMPONENT_MISSING(org.talend.sdk.component.server.front.model.ErrorDictionary.COMPONENT_MISSING) DesignModel(org.talend.sdk.component.design.extension.DesignModel) ComponentIndex(org.talend.sdk.component.server.front.model.ComponentIndex) FileInputStream(java.io.FileInputStream) ComponentIndices(org.talend.sdk.component.server.front.model.ComponentIndices) IconResolver(org.talend.sdk.component.server.service.IconResolver) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) ComponentDao(org.talend.sdk.component.server.dao.ComponentDao) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) InputStream(java.io.InputStream) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) WebApplicationException(javax.ws.rs.WebApplicationException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) File(java.io.File) Artifact(org.talend.sdk.component.dependencies.maven.Artifact) FileInputStream(java.io.FileInputStream) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ErrorPayload (org.talend.sdk.component.server.front.model.error.ErrorPayload)10 Path (javax.ws.rs.Path)7 GET (javax.ws.rs.GET)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 Container (org.talend.sdk.component.container.Container)5 Produces (javax.ws.rs.Produces)4 Response (javax.ws.rs.core.Response)4 ComponentFamilyMeta (org.talend.sdk.component.runtime.manager.ComponentFamilyMeta)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 Locale (java.util.Locale)3 Optional.ofNullable (java.util.Optional.ofNullable)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ApplicationScoped (javax.enterprise.context.ApplicationScoped)3 Inject (javax.inject.Inject)3 Consumes (javax.ws.rs.Consumes)3 DefaultValue (javax.ws.rs.DefaultValue)3 POST (javax.ws.rs.POST)3 PathParam (javax.ws.rs.PathParam)3 QueryParam (javax.ws.rs.QueryParam)3