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
c546d2f5
Commit
c546d2f5
authored
Feb 02, 2017
by
Maria Julia Dias de Lima
Browse files
Nova versão da demo da API REST do CSBase em Java
parent
107d8b31
Changes
24
Hide whitespace changes
Inline
Side-by-side
.gitignore
0 → 100644
View file @
c546d2f5
.project
.settings/**
/.idea
*.iml
target
/build/
pom.xml
View file @
c546d2f5
...
...
@@ -2,9 +2,9 @@
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
br.puc-rio.tecgraf
</groupId>
<artifactId>
rest
test
</artifactId>
<version>
0.0.1
-SNAPSHOT
</version>
<groupId>
br.puc-rio.tecgraf
.csbase
</groupId>
<artifactId>
rest
-client-demo-java
</artifactId>
<version>
0.0.1
</version>
<packaging>
jar
</packaging>
<name>
resttest
</name>
...
...
@@ -12,48 +12,116 @@
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<jersey2.version>
2.19
</jersey2.version>
<jaxrs.version>
2.0.1
</jaxrs.version>
</properties>
<distributionManagement>
<repository>
<id>
tecgraf-releases
</id>
<name>
Tecgraf Releases
</name>
<url>
http://maven.tecgraf.puc-rio.br:8081/nexus/content/repositories/releases
</url>
</repository>
<snapshotRepository>
<id>
tecgraf-snapshots
</id>
<name>
Tecgraf Snapshots
</name>
<url>
http://maven.tecgraf.puc-rio.br:8081/nexus/content/repositories/snapshots
</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<!-- Compilação com Java 8 -->
<plugin>
<inherited>
true
</inherited>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.5
</version>
<configuration>
<source>
1.8
</source>
<target>
1.8
</target>
</configuration>
</plugin>
<plugin>
<artifactId>
maven-assembly-plugin
</artifactId>
<executions>
<execution>
<id>
make-assembly
</id>
<phase>
package
</phase>
<goals>
<goal>
single
</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>
Demo.CommandSubmissionClient
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
exec-maven-plugin
</artifactId>
<version>
1.3.2
</version>
<executions>
<execution>
<goals>
<goal>
java
</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>
Demo.CommandSubmissionClient
</mainClass>
<arguments>
<argument>
src/main/resources/config.properties
</argument>
</arguments>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>
src
</directory>
<includes>
<include>
**/*.java
</include>
<include>
**/*.properties
</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<!-- JAX-RS -->
<dependency>
<groupId>
javax.ws.rs
</groupId>
<artifactId>
javax.ws.rs-api
</artifactId>
<version>
2.0.1
</version>
</dependency>
<dependency>
<groupId>
org.glassfish.jersey.containers
</groupId>
<artifactId>
jersey-container-servlet
</artifactId>
<version>
2.23.2
</version>
</dependency>
<dependency>
<groupId>
org.glassfish.jersey.media
</groupId>
<artifactId>
jersey-media-multipart
</artifactId>
<version>
2.24
</version>
<version>
${jaxrs.version}
</version>
</dependency>
<!-- if you are using Jersey client specific features without the server
side -->
<!-- Jersey -->
<dependency>
<groupId>
org.glassfish.jersey.core
</groupId>
<artifactId>
jersey-client
</artifactId>
<version>
2.23.2
</version>
<version>
${jersey2.version}
</version>
</dependency>
<dependency>
<groupId>
javax.json
</groupId>
<artifactId>
j
avax.json-api
</artifactId>
<version>
1.0
</version>
<groupId>
org.glassfish.jersey.media
</groupId>
<artifactId>
j
ersey-media-json-jackson
</artifactId>
<version>
${jersey2.version}
</version>
</dependency>
<dependency>
<groupId>
org.glassfish
</groupId>
<artifactId>
j
avax.json
</artifactId>
<version>
1.0.4
</version>
<groupId>
org.glassfish
.jersey.media
</groupId>
<artifactId>
j
ersey-media-multipart
</artifactId>
<version>
${jersey2.version}
</version>
</dependency>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>
com.google.code.gson
</groupId>
<artifactId>
gson
</artifactId>
<version>
2.7
</version>
<scope>
compile
</scope>
<groupId>
com.sun.jersey
</groupId>
<artifactId>
jersey-json
</artifactId>
<version>
1.19.3
</version>
</dependency>
</dependencies>
</dependencies>
</project>
src/main/java/Demo/CommandSubmissionClient.java
0 → 100644
View file @
c546d2f5
package
Demo
;
import
Requests.Algorithms.Algorithm
;
import
Requests.Algorithms.AlgorithmExplorer
;
import
Requests.Authentication.Authentication
;
import
Requests.Authentication.InvalidLoginOrPasswordException
;
import
Requests.Authentication.LoginOrPasswordNotProvidedException
;
import
Requests.Authentication.Token
;
import
Requests.Jobs.JobInfo
;
import
Requests.Jobs.JobServices
;
import
Requests.Projects.*
;
import
java.io.FileInputStream
;
import
java.io.InputStream
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Properties
;
public
class
CommandSubmissionClient
{
private
static
final
String
ALGORITHM
=
"multmatrix"
;
private
static
final
String
REMOTE_INPUT_FILE_1
=
"data_1.in"
;
private
static
final
String
REMOTE_INPUT_FILE_2
=
"data_2.in"
;
private
static
final
String
REMOTE_OUTPUT_FILE
=
"result.out"
;
/**
* Nome do arquivo com as propriedades para a demo
*/
private
static
String
configFileName
=
"config.properties"
;
/*
* Assertivas:
* - usuário já cadastrado
* - projeto previamente criado
* - usuário é o dono do projeto
* - algoritmo multmatrix cadastrado
* - arquivos de entrada disponiveis localmente
* - caminho válido para arquivo de saída a ser criado localmente
* - alguma maquina remota disponivel para executar
*/
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"-- Inicio da demo"
);
Properties
props
=
new
Properties
();
if
(
args
.
length
>
0
)
{
configFileName
=
args
[
0
];
try
{
InputStream
input
=
new
FileInputStream
(
configFileName
);
props
.
load
(
input
);
System
.
out
.
println
(
"Propriedades carregadas de "
+
configFileName
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Erro na leitura do arquivo "
+
configFileName
);
System
.
exit
(
1
);
}
}
else
{
InputStream
input
=
CommandSubmissionClient
.
class
.
getClassLoader
().
getResourceAsStream
(
configFileName
);
try
{
props
.
load
(
input
);
System
.
out
.
println
(
"Propriedades default carregadas"
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Erro na leitura do arquivo default "
+
configFileName
);
System
.
exit
(
1
);
}
}
String
host
=
props
.
getProperty
(
"host"
);
if
(
host
==
null
||
host
.
isEmpty
())
{
System
.
out
.
println
(
"A propriedade host e' obrigatoria"
);
System
.
exit
(
1
);
}
String
username
=
props
.
getProperty
(
"username"
);
if
(
host
==
null
||
host
.
isEmpty
())
{
System
.
out
.
println
(
"A propriedade username e' obrigatoria"
);
System
.
exit
(
1
);
}
String
password
=
props
.
getProperty
(
"password"
);
if
(
host
==
null
||
host
.
isEmpty
())
{
System
.
out
.
println
(
"A propriedade password e' obrigatoria"
);
System
.
exit
(
1
);
}
String
projectName
=
username
+
"/"
+
props
.
getProperty
(
"project"
);
if
(
host
==
null
||
host
.
isEmpty
())
{
System
.
out
.
println
(
"A propriedade project e' obrigatoria"
);
System
.
exit
(
1
);
}
String
projectFolderPath
=
props
.
getProperty
(
"projectFolderPath"
);
if
(
host
==
null
||
host
.
isEmpty
())
{
System
.
out
.
println
(
"A propriedade projectFolderPath e' obrigatoria"
);
System
.
exit
(
1
);
}
String
toUploadFilePath1
=
props
.
getProperty
(
"input_file_1"
);
if
(
host
==
null
||
host
.
isEmpty
())
{
System
.
out
.
println
(
"A propriedade input_file_1 e' obrigatoria"
);
System
.
exit
(
1
);
}
String
toUploadFilePath2
=
props
.
getProperty
(
"input_file_2"
);
if
(
host
==
null
||
host
.
isEmpty
())
{
System
.
out
.
println
(
"A propriedade input_file_2 e' obrigatoria"
);
System
.
exit
(
1
);
}
String
toSaveFilePath
=
props
.
getProperty
(
"output_file"
);
if
(
host
==
null
||
host
.
isEmpty
())
{
System
.
out
.
println
(
"A propriedade output_file e' obrigatoria"
);
System
.
exit
(
1
);
}
try
{
/*
* Autentica o usuario com login e senha configurados na demo
*/
Token
token
=
Authentication
.
authenticate
(
host
,
username
,
password
);
/*
* Procura o projeto com o nome configurado na demo
*/
Project
project
=
ProjectExplorer
.
findProjectByName
(
host
,
token
,
projectName
);
if
(
project
==
null
)
{
System
.
out
.
println
(
"Projeto "
+
projectName
+
" nao encontrado"
);
System
.
exit
(
1
);
}
System
.
out
.
println
(
"Projeto "
+
projectName
+
" encontrado"
);
/*
* Procura o algoritmo com o nome configurado na demo
*/
Algorithm
algorithm
=
AlgorithmExplorer
.
findAlgorithmByName
(
host
,
token
,
ALGORITHM
);
if
(
algorithm
==
null
)
{
System
.
out
.
println
(
"Algoritmo "
+
ALGORITHM
+
" nao encontrado"
);
System
.
exit
(
1
);
}
System
.
out
.
println
(
"Algoritmo "
+
ALGORITHM
+
" encontrado"
);
/*
* Faz o upload dos dados de entrada na raiz do projeto
*/
FileExplorer
.
uploadFile
(
host
,
token
,
project
,
projectFolderPath
,
toUploadFilePath1
,
REMOTE_INPUT_FILE_1
);
FileExplorer
.
uploadFile
(
host
,
token
,
project
,
projectFolderPath
,
toUploadFilePath2
,
REMOTE_INPUT_FILE_2
);
/*
* Executa o algoritmo Multmatrix
*/
Map
<
String
,
String
>
commandArgs
=
new
HashMap
<>();
commandArgs
.
put
(
"matrix1"
,
REMOTE_INPUT_FILE_1
);
commandArgs
.
put
(
"matrix2"
,
REMOTE_INPUT_FILE_2
);
commandArgs
.
put
(
"result"
,
REMOTE_OUTPUT_FILE
);
String
jobId
=
JobServices
.
submitJob
(
host
,
token
,
project
,
algorithm
,
algorithm
.
getVersions
().
get
(
0
),
"demo rest java"
,
0
,
false
,
new
String
[
0
],
commandArgs
);
if
(
jobId
==
null
)
{
System
.
out
.
println
(
"Falha na submissao do algoritmo "
+
ALGORITHM
);
System
.
exit
(
1
);
}
System
.
out
.
println
(
"Algoritmo "
+
ALGORITHM
+
" submetido com sucesso"
);
JobInfo
jinfo
=
JobServices
.
getJobInfo
(
host
,
token
,
jobId
);
if
(
jinfo
==
null
)
{
System
.
out
.
println
(
"Job "
+
jobId
+
" nao foi encontrado"
);
System
.
exit
(
1
);
}
System
.
out
.
println
(
"Job "
+
jobId
+
" encontrado"
);
/**
* Aguarda o job terminar sua execução
*/
JobServices
.
awaitJobEnd
(
host
,
token
,
jinfo
);
/**
* Faz download do arquivo de saída (quando o arquivo é pequeno)
*/
String
result
=
FileExplorer
.
downloadFile
(
host
,
token
,
project
,
projectFolderPath
,
REMOTE_OUTPUT_FILE
);
System
.
out
.
println
(
"Resultado em "
+
REMOTE_OUTPUT_FILE
+
":\n"
+
result
);
/**
* Faz download do arquivo de saída (quando o arquivo é grande)
*/
FileExplorer
.
downloadLargeFile
(
host
,
token
,
project
,
projectFolderPath
,
REMOTE_OUTPUT_FILE
,
toSaveFilePath
);
}
catch
(
LoginOrPasswordNotProvidedException
e
)
{
e
.
printStackTrace
();
}
catch
(
InvalidLoginOrPasswordException
e
)
{
e
.
printStackTrace
();
}
catch
(
ProjectOrFileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
PermissionException
e
)
{
e
.
printStackTrace
();
}
}
}
src/main/java/Helper/JSONHelper.java
deleted
100644 → 0
View file @
107d8b31
package
Helper
;
import
java.io.StringReader
;
import
java.lang.reflect.Array
;
import
javax.json.Json
;
import
javax.json.JsonArray
;
import
javax.json.JsonReader
;
import
com.google.gson.Gson
;
public
class
JSONHelper
{
public
static
<
T
>
T
[]
splitArray
(
String
jsonArray
,
Class
<
T
>
objClass
)
{
JsonReader
jsonReader
=
Json
.
createReader
(
new
StringReader
(
jsonArray
));
JsonArray
array
=
jsonReader
.
readArray
();
jsonReader
.
close
();
@SuppressWarnings
(
"unchecked"
)
T
[]
objects
=
(
T
[])
Array
.
newInstance
(
objClass
,
array
.
size
());
for
(
int
i
=
0
;
i
<
array
.
size
();
i
++)
objects
[
i
]
=
objClass
.
cast
(
new
Gson
().
fromJson
(
array
.
get
(
i
).
toString
(),
objClass
));
return
objects
;
}
}
src/main/java/Requests/Algorithms/Algorithm.java
View file @
c546d2f5
...
...
@@ -2,33 +2,123 @@ package Requests.Algorithms;
import
Requests.User.User
;
public
class
Algorithm
{
private
String
id
;
private
String
name
;
private
String
description
;
private
User
who_created
;
private
AlgorithmVersion
lastVersion
;
private
AlgorithmVersion
[]
versions
;
public
Algorithm
()
{
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Objects
;
public
class
Algorithm
{
private
String
id
=
null
;
private
String
name
=
null
;
private
String
description
=
null
;
private
User
whoCreated
=
null
;
private
AlgorithmVersion
lastVersion
=
null
;
private
List
<
AlgorithmVersion
>
versions
=
new
ArrayList
<
AlgorithmVersion
>();
public
Algorithm
id
(
String
id
)
{
this
.
id
=
id
;
return
this
;
}
public
String
getId
()
{
return
id
;
}
public
void
setId
(
String
id
)
{
this
.
id
=
id
;
}
public
Algorithm
name
(
String
name
)
{
this
.
name
=
name
;
return
this
;
}
public
String
getName
()
{
return
this
.
name
;
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
Algorithm
description
(
String
description
)
{
this
.
description
=
description
;
return
this
;
}
public
String
getDescription
()
{
return
description
;
}
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
}
public
Algorithm
whoCreated
(
User
whoCreated
)
{
this
.
whoCreated
=
whoCreated
;
return
this
;
}
public
User
getWhoCreated
()
{
return
whoCreated
;
}
public
void
setWhoCreated
(
User
whoCreated
)
{
this
.
whoCreated
=
whoCreated
;
}
public
Algorithm
lastVersion
(
AlgorithmVersion
lastVersion
)
{
this
.
lastVersion
=
lastVersion
;
return
this
;
}
public
AlgorithmVersion
getLastVersion
()
{
return
this
.
lastVersion
;
return
lastVersion
;
}
public
String
getId
()
{
return
this
.
id
;
public
void
setLastVersion
(
AlgorithmVersion
lastVersion
)
{
this
.
lastVersion
=
lastVersion
;
}
public
Algorithm
versions
(
List
<
AlgorithmVersion
>
versions
)
{
this
.
versions
=
versions
;
return
this
;
}
public
Algorithm
addVersionsItem
(
AlgorithmVersion
versionsItem
)
{
this
.
versions
.
add
(
versionsItem
);
return
this
;
}
public
AlgorithmVersion
[]
getVersions
()
{
return
this
.
versions
;
public
List
<
AlgorithmVersion
>
getVersions
()
{
return
versions
;
}
public
void
setVersions
(
List
<
AlgorithmVersion
>
versions
)
{
this
.
versions
=
versions
;
}
@Override
public
boolean
equals
(
java
.
lang
.
Object
o
)
{
if
(
this
==
o
)
{
return
true
;
}
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
{
return
false
;
}
Algorithm
algorithm
=
(
Algorithm
)
o
;
return
Objects
.
equals
(
this
.
id
,
algorithm
.
id
)
&&
Objects
.
equals
(
this
.
name
,
algorithm
.
name
)
&&
Objects
.
equals
(
this
.
description
,
algorithm
.
description
)
&&
Objects
.
equals
(
this
.
whoCreated
,
algorithm
.
whoCreated
)
&&
Objects
.
equals
(
this
.
lastVersion
,
algorithm
.
lastVersion
)
&&
Objects
.
equals
(
this
.
versions
,
algorithm
.
versions
);
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
id
,
name
,
description
,
whoCreated
,
lastVersion
,
versions
);
}
@Override
public
String
toString
()
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"class Algorithm {\n"
);
sb
.
append
(
" id: "
).
append
(
toIndentedString
(
id
)).
append
(
"\n"
);
sb
.
append
(
" name: "
).
append
(
toIndentedString
(
name
)).
append
(
"\n"
);
sb
.
append
(
" description: "
).
append
(
toIndentedString
(
description
)).
append
(
"\n"
);
sb
.
append
(
" whoCreated: "
).
append
(
toIndentedString
(
whoCreated
)).
append
(
"\n"
);
sb
.
append
(
" lastVersion: "
).
append
(
toIndentedString
(
lastVersion
)).
append
(
"\n"
);
sb
.
append
(
" versions: "
).
append
(
toIndentedString
(
versions
)).
append
(
"\n"
);
sb
.
append
(
"}"
);
return
sb
.
toString
();
}
private
String
toIndentedString
(
java
.
lang
.
Object
o
)
{
if
(
o
==
null
)
{
return
"null"
;
}
return
o
.
toString
().
replace
(
"\n"
,
"\n "
);
}
}
src/main/java/Requests/Algorithms/AlgorithmExplorer.java
View file @
c546d2f5
package
Requests.Algorithms
;
import
Requests.Authentication.Token
;
import
javax.ws.rs.client.Client
;
import
javax.ws.rs.client.ClientBuilder
;
import
javax.ws.rs.client.WebTarget
;
import
javax.ws.rs.core.HttpHeaders
;
import
javax.ws.rs.core.Response
;
import
Helper.JSONHelper
;
import
Requests.Authentication.Token
;
public
class
AlgorithmExplorer
{
public
static
Algorithm
findAlgorithm
(
String
host
,
Token
token
,
String
algorithmName
)
{
public
static
Algorithm
findAlgorithm
ByName
(
String
host
,
Token
token
,
String
algorithmName
)
{
Client
client
=
ClientBuilder
.
newClient
();
WebTarget
webTarget
=
client
.
target
(
host
);
Response
response
=
webTarget
.
path
(
"algorithms"
).
queryParam
(
"name"
,
algorithmName
)
Response
response
=
webTarget
.
path
(
"algorithms"
).
path
(
algorithmName
)
.
request
(
"application/json;charset=UTF-8"
)
.
header
(
HttpHeaders
.
AUTHORIZATION
,
token
.
getTokenType
()
+
token
.
getAccessToken
()).
get
();
if
(
response
.
getStatus
()
==
Response
.
Status
.
OK
.
getStatusCode
())
{
Algorithm
[]
algorithms
=
getAlgorithms
(
response
.
readEntity
(
String
.
class
));
for
(
Algorithm
algo:
algorithms
)
if
(
algo
.
getName
().
equals
(
algorithmName
))
return
algo
;
return
response
.
readEntity
(
Algorithm
.
class
);
}
return
null
;
}
...
...
@@ -38,11 +33,8 @@ public class AlgorithmExplorer {
.
header
(
HttpHeaders
.
AUTHORIZATION
,
token
.
getTokenType
()
+
token
.
getAccessToken
()).
get
();
if
(
response
.
getStatus
()
==
Response
.
Status
.
OK
.
getStatusCode
())
return
getAlgorithms
(
response
.
readEntity
(
String
.
class
)
)
;
return
response
.
readEntity
(
Algorithm
[]
.
class
);
return
null
;
}
private
static
Algorithm
[]
getAlgorithms
(
String
jsonArray
)
{
return
JSONHelper
.
splitArray
(
jsonArray
,
Algorithm
.
class
);
}
}
src/main/java/Requests/Algorithms/AlgorithmVersion.java
View file @
c546d2f5
package
Requests.Algorithms
;
public
class
AlgorithmVersion
{
private
String
id
;
private
String
description
;
private
Requirement
[]
requirements
;
public
AlgorithmVersion
()
{
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Objects
;
public
class
AlgorithmVersion
{
private
String
id
=
null
;
private
String
description
=
null
;
private
List
<
Requirement
>
requirements
=
new
ArrayList
<
Requirement
>();
public
AlgorithmVersion
id
(
String
id
)
{
this
.
id
=
id
;
return
this
;
}
public
String
getId
()
{
return
this
.
id
;
return
id
;
}