Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
csbase-dev
rest-client-demo-java
Commits
ef265f78
Commit
ef265f78
authored
Nov 22, 2016
by
Erica Freitas de Linhares e Riello
Browse files
parametros de acordo com documentação
parent
7e06f4d2
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/Requests/Jobs/JobServices.java
View file @
ef265f78
...
...
@@ -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
;
}
...
...
src/main/java/Requests/Projects/FileExplorer.java
View file @
ef265f78
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
ProjectOrFileNotFoun
dException
();
throw
new
InvalidLoginOrPasswor
dException
();
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
);
}
}
target/classes/Requests/Jobs/JobServices.class
View file @
ef265f78
No preview for this file type
target/classes/Requests/Projects/FileExplorer.class
View file @
ef265f78
No preview for this file type
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment