Commit ef265f78 authored by Erica Freitas de Linhares e Riello's avatar Erica Freitas de Linhares e Riello
Browse files

parametros de acordo com documentação

parent 7e06f4d2
......@@ -58,6 +58,8 @@ public class JobServices {
.post(Entity.entity(body, MediaType.APPLICATION_JSON_TYPE), Response.class);
if (response.getStatus() == Response.Status.OK.getStatusCode())
return response.readEntity(String.class);
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));
return null;
}
......
package Requests.Projects;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.text.Normalizer;
import java.util.Base64;
import java.util.regex.Pattern;
......@@ -23,6 +29,7 @@ import org.glassfish.jersey.media.multipart.internal.MultiPartWriter;
import com.google.gson.Gson;
import Requests.Authentication.InvalidLoginOrPasswordException;
import Requests.Authentication.Token;
public class FileExplorer {
......@@ -87,11 +94,13 @@ public class FileExplorer {
}
public static String downloadFile(String host, Token token, Project project, String projectFolderPath,
String fileName) throws ProjectOrFileNotFoundException, PermissionException {
String fileName)
throws ProjectOrFileNotFoundException, PermissionException, InvalidLoginOrPasswordException {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(host);
String fileId = Base64.getEncoder().encodeToString(projectFolderPath.equals("root") ? fileName.getBytes()
: (projectFolderPath + "/" + fileName).getBytes());
: (projectFolderPath + (fileName == null ? "" : "/" + fileName)).getBytes());
System.out.println(fileId);
WebTarget uploadTarget = webTarget.path("projects").path(project.getId()).path("files").path(fileId);
Invocation.Builder invocationBuilder = uploadTarget.request();
......@@ -104,11 +113,64 @@ public class FileExplorer {
if (status == Response.Status.OK.getStatusCode())
return response.readEntity(String.class);
else if (status == Response.Status.BAD_REQUEST.getStatusCode())
throw new ProjectOrFileNotFoundException();
throw new InvalidLoginOrPasswordException();
else if (status == Response.Status.FORBIDDEN.getStatusCode())
throw new PermissionException();
else if (status == Response.Status.NOT_FOUND.getStatusCode())
throw new ProjectOrFileNotFoundException();
return null;
}
public static void downloadLargeFile(String host, Token token, Project project, String projectFolderPath,
String fileName, String destinFileName)
throws ProjectOrFileNotFoundException, PermissionException, InvalidLoginOrPasswordException {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(host);
String fileId = Base64.getEncoder().encodeToString(projectFolderPath.equals("root") ? fileName.getBytes()
: (projectFolderPath + (fileName == null ? "" : "/" + fileName)).getBytes());
System.out.println(fileId);
WebTarget uploadTarget = webTarget.path("projects").path(project.getId()).path("files").path(fileId)
.path("link");
Invocation.Builder invocationBuilder = uploadTarget.request("application/json;charset=UTF-8");
Response response = invocationBuilder
.header(HttpHeaders.AUTHORIZATION, token.getTokenType() + token.getAccessToken()).get();
int status = response.getStatus();
if (status == Response.Status.OK.getStatusCode()) {
FileLink link = new Gson().fromJson(response.readEntity(String.class), FileLink.class);
URL website;
try {
File file = new File(destinFileName);
file.getParentFile().mkdirs();
file.createNewFile();
website = new URL(link.getURL());
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream(destinFileName);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (status == Response.Status.BAD_REQUEST.getStatusCode())
throw new InvalidLoginOrPasswordException();
else if (status == Response.Status.FORBIDDEN.getStatusCode())
throw new PermissionException();
else if (status == Response.Status.NOT_FOUND.getStatusCode())
throw new ProjectOrFileNotFoundException();
System.out.println(status);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment