All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jk.services.client.JKFileDownloadServiceCaller Maven / Gradle / Ivy

There is a newer version: 7.0.0-M7
Show newest version
/*
 * 
 */
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";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy