com.jk.services.client.JKFileDownloadServiceCaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jk-framework-service-client Show documentation
Show all versions of jk-framework-service-client Show documentation
A simple API to call Microservices from Java
/*
*
*/
package com.jk.services.client;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.jk.core.util.JK;
import com.jk.core.util.JKIOUtil;
import com.jk.core.util.JKObjectUtil;
// TODO: Auto-generated Javadoc
/**
* The Class JKFileDownloadServiceCaller.
*/
public class JKFileDownloadServiceCaller implements JKServiceCaller {
/** The object. */
private Object object;
/**
* Instantiates a new JK file download service caller.
*
* @param object the object
*/
public JKFileDownloadServiceCaller(Object object) {
this.object = object;
}
/**
* Call.
*
* @param builder the builder
* @return the response
*/
@Override
public Response call(Builder builder) {
return builder.post(Entity.entity(JKObjectUtil.toJson(object), MediaType.APPLICATION_JSON));
}
/**
* Read response.
*
* @param response the response
* @return the object
*/
@Override
public Object readResponse(Response response) {
try {
File file = JKIOUtil.createTempFile("zip");
InputStream in = response.readEntity(InputStream.class);
long bytesCopied = Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
e.printStackTrace();
JK.throww(e);
return null;
}
}
/**
* Gets the accept media.
*
* @return the accept media
*/
@Override
public String getAcceptMedia() {
return "application/zip";
}
}