File Upload Using Rest API Client in JAVA - java

I have created a client rest api to upload a file on the api endpoints. But this is giving me error:
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:191)
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:139)
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1005)
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:430)
at org.glassfish.jersey.client.HttpUrlConnector._apply(HttpUrlConnector.java:287)
at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:200)
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:215)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:635)
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:632)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:421)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:632)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:392)
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:301)
at Test.main(Test.java:35)
Here is my client rest api code:
import java.io.File;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
public class Test {
public void configureClient(ClientConfig config) {
config.register(MultiPartFeature.class);
}
public static void main(String[] args) {
Client client = ClientBuilder.newClient().register(MultiPart.class);
WebTarget server = client.target("http://localhost:8080/repositories/file/upload");
FileDataBodyPart filePart = new FileDataBodyPart("file", new File("C:\\Users\\kunal\\Downloads\\JWTUtility.java"));
filePart.setContentDisposition(FormDataContentDisposition.name("file").fileName("JWTUtility.java").build());
MultiPart multipartEntity = new FormDataMultiPart().bodyPart(filePart);
Response result = server.request().post(Entity.entity(multipartEntity, multipartEntity.getMediaType()));
System.out.println(result.getStatus());
System.out.println(result.readEntity(String.class));
result.close();
//assertEquals(Response.Status.OK.getStatusCode(), result.getStatus());
}
}
API Endpoint code:
package com.howtodoinjava.jersey;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
#Path("/upload")
public class JerseyService
{
#POST
#Path("/pdf")
#Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadPdfFile( #FormDataParam("file") InputStream fileInputStream,
#FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception
{
String UPLOAD_PATH = "D://Test/";
try
{
int read = 0;
byte[] bytes = new byte[1024];
OutputStream out = new FileOutputStream(new File(UPLOAD_PATH + fileMetaData.getFileName()));
while ((read = fileInputStream.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e)
{
throw new WebApplicationException("Error while uploading file. Please try again !!");
}
return Response.ok("Data uploaded successfully !!").build();
}
}

In the client side, you need to add support for the multipart/form-data feature which will register the needed reader and writer:
public class Test {
public void configureClient(ClientConfig config) {
config.register(MultiPartFeature.class);
}
public static void main(String[] args) {
DefaultClientConfig config = new DefaultClientConfig();
configureClient(config);
Client client = ClientBuilder.newClient(config);
WebTarget server = client.target("http://localhost:8080/repositories/file/upload");
// or alternatively at the WebTarget level
// server.register(MultiPartFeature.class);
// ...
}
}

Related

Prometheus 'expected label name, got "BCLOSE"' error

I have an application running on port 7070 on my local. It exposes and endpoint /metrics and shows all the tags that are available. Prometheus is not able to get these data and it says 'expected label name, got "BCLOSE"'.
I have been trying to figure this out but not sure why this code doesn't work:
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import metrics.PrometheusRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
#Path("/metrics")
public class MetricsController {
private final PrometheusMeterRegistry prometheusRegistry = PrometheusRegistry.INSTANCE();
#GET
public Response getMetrics(#Context HttpHeaders headers) {
Writer writer = new StringWriter();
try {
TextFormat.write004(writer, prometheusRegistry.getPrometheusRegistry().metricFamilySamples());
} catch (IOException e) {
e.printStackTrace();
}
return writer.toString();
}
}
Also, the application is neither a sprintboot nor a spring project.
Tried this:
#GET
public Response getMetrics(#Context HttpHeaders headers) {
String accept = headers.getRequestHeader("Accept").get(0);
System.out.println("Accept Header --------------------------> " + accept);
return Response.ok(prometheusRegistry.scrape(), "application/openmetrics-text").build();
}
Even then the same error as above SS.
This worked for me:
#GET
public Response getMetrics() {
return Response.ok(prometheusRegistry.scrape(), TextFormat.CONTENT_TYPE_004).build();
}

RestEasy Mock unit test with CDI

I've a simple RestEasy WebServie which will take a dependency.
When I test by publishing into Wildfly server everything is working fine. But I tried to mock it using JUnit. But, it's throwing null pointer exception as the dependency isn't injected.
Following is the code which I've done. What is wrong I'm doing.
WebService;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
#Path("/")
public class IlService {
#Inject
private il.helper.RequestData rData;
#GET
public Response postIds() {
return Response.status( 200 ).type( MediaType.TEXT_HTML ).entity( rData.getRequestData().get( "logicalOperator" ).asText() ).build();
}
}
JUnit Test class:
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.spi.InjectorFactory;
import org.jboss.resteasy.spi.PropertyInjector;
import org.jboss.resteasy.spi.Registry;
import org.jboss.resteasy.spi.ResourceFactory;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.junit.Before;
import org.junit.Test;
import il.helper.RequestData;
import il.services.IlService;
import junit.framework.Assert;
public class IlServiceTest {
private Dispatcher dispatcher;
ResteasyProviderFactory resteasyProviderFactory;
ResourceFactory factory;
InjectorFactory injectorFactory;
PropertyInjector propertyInjector;
#Before
public void setUpResource() {
dispatcher = MockDispatcherFactory.createDispatcher();
resteasyProviderFactory = dispatcher.getProviderFactory();
injectorFactory = resteasyProviderFactory.getInjectorFactory();
propertyInjector = injectorFactory.createPropertyInjector( IlService.class, resteasyProviderFactory );
propertyInjector.inject( new RequestData() );
resteasyProviderFactory.setInjectorFactory( injectorFactory );
Registry registry = dispatcher.getRegistry();
registry.addSingletonResource( new IlService() );
}
#Test
public void IlServicePost() {
try {
MockHttpRequest request = MockHttpRequest.create( "get", "" );
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke( request, response );
Assert.assertEquals( 200, response.getStatus() );
} catch ( Exception e ) {
e.printStackTrace();
}
}
}

How to execute rest api from java and capture the response?

Executing this url on my browser: http://localhost:3161/devices/simulator/stop
I don't need login for it. It returns this rest api xml:
<response>
<type>response</type>
<ts>1463749194000</ts>
<status>OK</status>
<msg-version>2.3.0</msg-version>
<op>stop</op>
<data/>
</response>
How can I execute this from JAVA and then capture the xml response?
As other mentioned in this post, it is generic thing, you would be able to find it online already..
I know there are clients to call the REST services from Java. Two of them are listed for your case.
case -1 : if you are using Jersey REST API. Here to capture XML , you can go with your own way,for example use JAXB and XML elements to Java Bean properties.
import java.io.IOException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.http.client.ClientProtocolException;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class Test {
public static void main(String[] args) throws ClientProtocolException, IOException {
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(UriBuilder.fromUri('http://localhost:3161/devices/simulator/stop').build());
// getting XML data
System.out.println(service. path('restPath').path('resourcePath').accept(MediaType.APPLICATION_JSON).get(String.class));
// getting JSON data
System.out.println(service. path('restPath').path('resourcePath').accept(MediaType.APPLICATION_XML).get(String.class));
}
}
Case 2: using HTTP method, it is simple method but parse XML instead of printing it here
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class Test {
public static void main(String[] args) throws ClientProtocolException, IOException {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet('http://localhost:3161/devices/simulator/stop');
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
String line = '';
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
}
}

Java jax - rs program to accept files from external systems

I am trying to create a simple Java Jax-RS based webservice which accepts the file in a byte[] format or as a blob and pushes it to an FTP folder. This service is used by Salesforce to push files to directories via this Java API. For testing i have hosted this application in https://ftptransfer.herokuapp.com/myresource
I need to create a class which basically accepts files from external systems
package com.example;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.POST;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
#Path("sendFiles")
public class FileTransfer {
#POST
#Consumes({MediaType.MULTIPART_FORM_DATA})
public String uploadPdfFile( )
{
return "Uploaded successfully";
}
}
Can some help with any good reference to achieve this, as i have tried many approaches from different blogs but none helped me much.
I am able to achieve this in the following way
import org.apache.commons.net.ftp.FTPClient;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import java.io.InputStream;
import javax.ws.rs.POST;
#Path("sendFiles")
public class FileTransfer {
#POST
#Path("file")
#Consumes("*/*")
public String uploadPdfFile(InputStream fileInputStream)
{
FTPClient client = new FTPClient();
boolean login;
FileInputStream fis = null;
int read = 0;
byte[] bytes = new byte[1024];
OutputStream out;
try {
File file=new File("testhm001.txt");
out = new FileOutputStream(file);
while ((read = fileInputStream.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
out.flush();
out.close();
InputStream in = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out1 = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out1.append(line);
}
return "done";
}
}

java unit test mock HttpClient and webdav

Hello I have a class for doing webdav related operations such as creating a directory, Implementatiion can be seen below (the createDir method). The question is how to test it nicely, perhaps using EasyMock or a similar lib. Any ideas? thanks!
package foobar;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.jackrabbit.webdav.client.methods.DavMethod;
import org.apache.jackrabbit.webdav.client.methods.MkColMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import mypackage.httpdclient.util.URLHandler;
public class WebDavImpl{
private static final String SEPARATOR = " ----- ";
private HttpClient httpClient;
public StorageSpaceClientImpl() {
httpClient = new HttpClient();
}
public String createDir(String dirName) {
String response = null;
String url = URLHandler.getInstance().getDirectoryUrl(dirName);
DavMethod mkcol = new MkColMethod(url);
try {
httpClient.executeMethod(mkcol);
response = mkcol.getStatusCode() + SEPARATOR + mkcol.getStatusText();
} catch (IOException ex) {
} finally {
mkcol.releaseConnection();
}
return response;
}
}

Categories