Search in sources :

Example 6 with Providers

use of javax.ws.rs.ext.Providers in project component-runtime by Talend.

the class ProjectResource method createZip.

@POST
@Path("zip/form")
@Produces("application/zip")
public Response createZip(@FormParam("project") final String compressedModel, @Context final Providers providers) {
    final ProjectModel model = readProjectModel(compressedModel, providers);
    final String filename = ofNullable(model.getArtifact()).orElse("zip") + ".zip";
    return Response.ok().entity((StreamingOutput) out -> {
        generator.generate(toRequest(model), out);
        out.flush();
    }).header("Content-Disposition", "inline; filename=" + filename).build();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Produces(javax.ws.rs.Produces) ErrorMessage(org.talend.sdk.component.starter.server.model.ErrorMessage) Path(javax.ws.rs.Path) GithubProject(org.talend.sdk.component.starter.server.model.GithubProject) Collections.singletonList(java.util.Collections.singletonList) MediaType(javax.ws.rs.core.MediaType) Collectors.toMap(java.util.stream.Collectors.toMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Consumes(javax.ws.rs.Consumes) Locale(java.util.Locale) Map(java.util.Map) ENGLISH(java.util.Locale.ENGLISH) ZipEntry(java.util.zip.ZipEntry) ProjectGenerator(org.talend.sdk.component.starter.server.service.ProjectGenerator) Context(javax.ws.rs.core.Context) Providers(javax.ws.rs.ext.Providers) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) StreamingOutput(javax.ws.rs.core.StreamingOutput) NullProgressMonitor(org.eclipse.jgit.lib.NullProgressMonitor) Collectors.joining(java.util.stream.Collectors.joining) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) Base64(java.util.Base64) List(java.util.List) FileUtils(org.eclipse.jgit.util.FileUtils) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) Function.identity(java.util.function.Function.identity) Writer(java.io.Writer) Annotation(java.lang.annotation.Annotation) PostConstruct(javax.annotation.PostConstruct) WebApplicationException(javax.ws.rs.WebApplicationException) ProjectModel(org.talend.sdk.component.starter.server.model.ProjectModel) ApplicationScoped(javax.enterprise.context.ApplicationScoped) CreateProjectRequest(org.talend.sdk.component.starter.server.model.github.CreateProjectRequest) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) GET(javax.ws.rs.GET) Client(javax.ws.rs.client.Client) HashMap(java.util.HashMap) Entity.entity(javax.ws.rs.client.Entity.entity) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ClientBuilder(javax.ws.rs.client.ClientBuilder) StarterConfiguration(org.talend.sdk.component.starter.server.configuration.StarterConfiguration) FactoryConfiguration(org.talend.sdk.component.starter.server.model.FactoryConfiguration) Collections.emptyMap(java.util.Collections.emptyMap) FormParam(javax.ws.rs.FormParam) POST(javax.ws.rs.POST) Files(java.nio.file.Files) Optional.ofNullable(java.util.Optional.ofNullable) FileWriter(java.io.FileWriter) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) File(java.io.File) CreateProjectResponse(org.talend.sdk.component.starter.server.model.github.CreateProjectResponse) TimeUnit(java.util.concurrent.TimeUnit) Result(org.talend.sdk.component.starter.server.model.Result) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) BufferedReader(java.io.BufferedReader) Git(org.eclipse.jgit.api.Git) Comparator(java.util.Comparator) ProjectRequest(org.talend.sdk.component.starter.server.service.domain.ProjectRequest) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) ProjectModel(org.talend.sdk.component.starter.server.model.ProjectModel) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 7 with Providers

use of javax.ws.rs.ext.Providers in project cxf by apache.

the class DOM4JProviderTest method testWriteXMLSuppressDeclaration.

@Test
public void testWriteXMLSuppressDeclaration() throws Exception {
    org.dom4j.Document dom = readXML(MediaType.APPLICATION_XML_TYPE, "<a/>");
    final Message message = createMessage(true);
    Providers providers = new ProvidersImpl(message);
    DOM4JProvider p = new DOM4JProvider() {

        protected Message getCurrentMessage() {
            return message;
        }
    };
    p.setProviders(providers);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String str = bos.toString();
    assertFalse(str.startsWith("<?xml"));
    assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
}
Also used : Message(org.apache.cxf.message.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) Test(org.junit.Test)

Example 8 with Providers

use of javax.ws.rs.ext.Providers in project cxf by apache.

the class DOM4JProviderTest method doTestWriteXML.

private void doTestWriteXML(MediaType ct, boolean convert) throws Exception {
    org.dom4j.Document dom = readXML(ct, "<a/>");
    final Message message = createMessage(false);
    Providers providers = new ProvidersImpl(message);
    DOM4JProvider p = new DOM4JProvider() {

        protected Message getCurrentMessage() {
            return message;
        }
    };
    p.setProviders(providers);
    p.convertToDOMAlways(convert);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    p.writeTo(dom, org.dom4j.Document.class, org.dom4j.Document.class, new Annotation[] {}, ct, new MetadataMap<String, Object>(), bos);
    String str = bos.toString();
    if (convert) {
        assertFalse(str.startsWith("<?xml"));
    } else {
        assertTrue(str.startsWith("<?xml"));
    }
    assertTrue(str.contains("<a/>") || str.contains("<a></a>"));
}
Also used : Message(org.apache.cxf.message.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl)

Example 9 with Providers

use of javax.ws.rs.ext.Providers in project jersey by jersey.

the class JaxrsProvidersTest method testProviders.

@Test
public void testProviders() throws Exception {
    final InjectionManager injectionManager = Injections.createInjectionManager(new MessagingBinders.MessageBodyProviders(null, RuntimeType.SERVER), new Binder());
    injectionManager.register(new TestBinder(injectionManager));
    TestBinder.initProviders(injectionManager);
    RequestScope scope = injectionManager.getInstance(RequestScope.class);
    scope.runInScope(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Providers instance = injectionManager.getInstance(Providers.class);
            assertNotNull(instance);
            assertSame(JaxrsProviders.class, instance.getClass());
            assertNotNull(instance.getExceptionMapper(Throwable.class));
            assertNotNull(instance.getMessageBodyReader(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
            assertNotNull(instance.getMessageBodyWriter(String.class, String.class, new Annotation[0], MediaType.TEXT_PLAIN_TYPE));
            assertNotNull(instance.getContextResolver(String.class, MediaType.TEXT_PLAIN_TYPE));
            return null;
        }
    });
}
Also used : AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) MessagingBinders(org.glassfish.jersey.message.internal.MessagingBinders) Providers(javax.ws.rs.ext.Providers) RequestScope(org.glassfish.jersey.process.internal.RequestScope) InjectionManager(org.glassfish.jersey.internal.inject.InjectionManager) Test(org.junit.Test)

Example 10 with Providers

use of javax.ws.rs.ext.Providers in project component-runtime by Talend.

the class ExecutionResource method read.

/**
 * Read inputs from an instance of mapper. The number of returned records if enforced to be limited to 1000.
 * The format is a JSON based format where each like is a json record.
 *
 * @param family the component family.
 * @param component the component name.
 * @param size the maximum number of records to read.
 * @param configuration the component configuration as key/values.
 */
@POST
@Deprecated
@Produces("talend/stream")
@Path("read/{family}/{component}")
public void read(@Suspended final AsyncResponse response, @Context final Providers providers, @PathParam("family") final String family, @PathParam("component") final String component, @QueryParam("size") @DefaultValue("50") final long size, final Map<String, String> configuration) {
    final long maxSize = Math.min(size, MAX_RECORDS);
    response.setTimeoutHandler(asyncResponse -> log.warn("Timeout on dataset retrieval"));
    response.setTimeout(appConfiguration.datasetRetrieverTimeout(), SECONDS);
    executorService.submit(() -> {
        final Optional<Mapper> mapperOptional = manager.findMapper(family, component, getConfigComponentVersion(configuration), configuration);
        if (!mapperOptional.isPresent()) {
            response.resume(new WebApplicationException(Response.status(BAD_REQUEST).entity(new ErrorPayload(COMPONENT_MISSING, "Didn't find the input component")).build()));
            return;
        }
        final Mapper mapper = mapperOptional.get();
        mapper.start();
        try {
            final Input input = mapper.create();
            try {
                input.start();
                response.resume((StreamingOutput) output -> {
                    Object data;
                    int current = 0;
                    while (current++ < maxSize && (data = input.next()) != null) {
                        if (CharSequence.class.isInstance(data) || Number.class.isInstance(data) || Boolean.class.isInstance(data)) {
                            final PrimitiveWrapper wrapper = new PrimitiveWrapper();
                            wrapper.setValue(data);
                            data = wrapper;
                        }
                        inlineStreamingMapper.toJson(data, output);
                        output.write(EOL);
                    }
                });
            } finally {
                input.stop();
            }
        } finally {
            mapper.stop();
        }
    });
}
Also used : PrimitiveWrapper(org.talend.sdk.component.server.front.model.execution.PrimitiveWrapper) Produces(javax.ws.rs.Produces) Path(javax.ws.rs.Path) BAD_FORMAT(org.talend.sdk.component.server.front.model.ErrorDictionary.BAD_FORMAT) PreDestroy(javax.annotation.PreDestroy) MediaType(javax.ws.rs.core.MediaType) JsonNumber(javax.json.JsonNumber) QueryParam(javax.ws.rs.QueryParam) Collectors.toMap(java.util.stream.Collectors.toMap) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) JsonObject(javax.json.JsonObject) JsonbBuilder(javax.json.bind.JsonbBuilder) Context(javax.ws.rs.core.Context) Providers(javax.ws.rs.ext.Providers) AsyncResponse(javax.ws.rs.container.AsyncResponse) StreamingOutput(javax.ws.rs.core.StreamingOutput) Processor(org.talend.sdk.component.runtime.output.Processor) Suspended(javax.ws.rs.container.Suspended) StandardCharsets(java.nio.charset.StandardCharsets) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) OutputEmitter(org.talend.sdk.component.api.processor.OutputEmitter) Branches(org.talend.sdk.component.runtime.output.Branches) Slf4j(lombok.extern.slf4j.Slf4j) Response(javax.ws.rs.core.Response) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ACTION_ERROR(org.talend.sdk.component.server.front.model.ErrorDictionary.ACTION_ERROR) WriteStatistics(org.talend.sdk.component.server.front.model.execution.WriteStatistics) PathParam(javax.ws.rs.PathParam) OutputFactory(org.talend.sdk.component.runtime.output.OutputFactory) Inject(javax.inject.Inject) ComponentServerConfiguration(org.talend.sdk.component.server.configuration.ComponentServerConfiguration) Input(org.talend.sdk.component.runtime.input.Input) ExecutorService(java.util.concurrent.ExecutorService) POST(javax.ws.rs.POST) Optional.ofNullable(java.util.Optional.ofNullable) COMPONENT_MISSING(org.talend.sdk.component.server.front.model.ErrorDictionary.COMPONENT_MISSING) InputStreamReader(java.io.InputStreamReader) JsonString(javax.json.JsonString) Mapper(org.talend.sdk.component.runtime.input.Mapper) Jsonb(javax.json.bind.Jsonb) BufferedReader(java.io.BufferedReader) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) SECONDS(java.util.concurrent.TimeUnit.SECONDS) InputStream(java.io.InputStream) Mapper(org.talend.sdk.component.runtime.input.Mapper) PrimitiveWrapper(org.talend.sdk.component.server.front.model.execution.PrimitiveWrapper) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Input(org.talend.sdk.component.runtime.input.Input) WebApplicationException(javax.ws.rs.WebApplicationException) JsonNumber(javax.json.JsonNumber) JsonObject(javax.json.JsonObject) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Aggregations

Providers (javax.ws.rs.ext.Providers)14 MediaType (javax.ws.rs.core.MediaType)7 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Map (java.util.Map)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)4 BufferedReader (java.io.BufferedReader)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 Annotation (java.lang.annotation.Annotation)3 StandardCharsets (java.nio.charset.StandardCharsets)3 List (java.util.List)3 Inject (javax.inject.Inject)3 Response (javax.ws.rs.core.Response)3 InjectionManager (org.glassfish.jersey.internal.inject.InjectionManager)3 BufferedInputStream (java.io.BufferedInputStream)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2