Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
synergy-api-util
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Raimbek Egemberdiev
synergy-api-util
Commits
81f08031
Commit
81f08031
authored
Nov 10, 2016
by
Raimbek Egemberdiev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
queries
parent
034811a3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
140 additions
and
17 deletions
+140
-17
src/main/java/kz/arta/synergy/api/Query.java
src/main/java/kz/arta/synergy/api/Query.java
+92
-0
src/main/java/kz/arta/synergy/api/RestHttpQuery.java
src/main/java/kz/arta/synergy/api/RestHttpQuery.java
+27
-12
src/main/java/kz/arta/synergy/api/asforms/AsFormProvider.java
...main/java/kz/arta/synergy/api/asforms/AsFormProvider.java
+21
-5
No files found.
src/main/java/kz/arta/synergy/api/Query.java
0 → 100644
View file @
81f08031
package
kz.arta.synergy.api
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author raimbek
* @since 10.11.2016
*/
public
class
Query
{
private
String
url
;
private
Map
<
String
,
String
>
headers
=
new
HashMap
<>();
private
Map
<
String
,
String
>
queryParams
=
new
HashMap
<>();
private
Map
<
String
,
String
>
formParams
=
new
HashMap
<>();
private
String
body
;
private
String
method
=
"GET"
;
private
Query
()
{
}
public
static
Query
newInstance
()
{
return
new
Query
();
}
public
Query
url
(
String
url
)
{
this
.
url
=
url
;
return
this
;
}
public
Query
queryParam
(
String
key
,
String
value
)
{
queryParams
.
put
(
key
,
value
);
return
this
;
}
public
Query
formParam
(
String
key
,
String
value
)
{
formParams
.
put
(
key
,
value
);
return
this
;
}
public
Query
header
(
String
key
,
String
value
)
{
headers
.
put
(
key
,
value
);
return
this
;
}
public
Query
body
(
String
body
)
{
this
.
body
=
body
;
return
this
;
}
public
Query
methodPost
()
{
this
.
method
=
"POST"
;
return
this
;
}
public
Query
methodGet
()
{
this
.
method
=
"GET"
;
return
this
;
}
public
String
getUrl
()
{
return
url
;
}
public
Map
<
String
,
String
>
getHeaders
()
{
return
headers
;
}
public
Map
<
String
,
String
>
getQueryParams
()
{
return
queryParams
;
}
public
Map
<
String
,
String
>
getFormParams
()
{
return
formParams
;
}
public
String
getBody
()
{
if
(
body
==
null
)
{
return
""
;
}
return
body
;
}
public
String
getMethod
()
{
return
method
;
}
}
src/main/java/kz/arta/synergy/api/RestHttpQuery.java
View file @
81f08031
...
...
@@ -3,12 +3,10 @@ package kz.arta.synergy.api;
import
com.google.common.base.Function
;
import
com.google.common.base.Joiner
;
import
com.google.common.base.Predicates
;
import
com.google.common.base.Strings
;
import
com.google.common.collect.FluentIterable
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.UnsupportedEncodingException
;
import
java.io.*
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URLEncoder
;
...
...
@@ -28,14 +26,35 @@ public class RestHttpQuery {
this
.
context
=
context
;
}
public
String
do
GetQuery
(
String
query
)
throws
IOException
{
URL
url
=
new
URL
(
context
.
getAddress
()
+
query
);
public
String
do
Query
(
Query
query
)
throws
IOException
{
URL
url
=
new
URL
(
context
.
getAddress
()
+
makeQuery
(
query
.
getUrl
(),
query
.
getQueryParams
())
);
HttpURLConnection
conn
=
(
HttpURLConnection
)
url
.
openConnection
();
conn
.
setConnectTimeout
(
TIMEOUT
);
conn
.
setReadTimeout
(
TIMEOUT
);
conn
.
setRequestMethod
(
"GET"
);
conn
.
setRequestProperty
(
"Accept"
,
"application/json; charset=utf-8"
);
conn
.
setRequestMethod
(
query
.
getMethod
());
Map
<
String
,
String
>
headers
=
query
.
getHeaders
();
for
(
Map
.
Entry
<
String
,
String
>
header
:
headers
.
entrySet
())
{
conn
.
setRequestProperty
(
header
.
getKey
(),
header
.
getValue
());
}
authentication
(
conn
);
conn
.
setUseCaches
(
true
);
conn
.
setDoInput
(
true
);
if
(
query
.
getMethod
().
equals
(
"POST"
))
{
conn
.
setDoOutput
(
true
);
DataOutputStream
request
=
new
DataOutputStream
(
conn
.
getOutputStream
());
if
(!
query
.
getFormParams
().
isEmpty
())
{
request
.
write
(
joinParamsForQuery
(
query
.
getFormParams
()).
getBytes
(
"UTF-8"
));
}
else
if
(!
Strings
.
isNullOrEmpty
(
query
.
getBody
()))
{
request
.
write
(
query
.
getBody
().
getBytes
(
"UTF-8"
));
}
request
.
flush
();
request
.
close
();
}
return
readStringReturn
(
conn
);
}
...
...
@@ -44,10 +63,6 @@ public class RestHttpQuery {
}*/
public
String
doGetQuery
(
String
query
,
Map
<
String
,
?>
params
)
throws
IOException
{
return
doGetQuery
(
makeQuery
(
query
,
params
));
}
private
String
makeQuery
(
String
query
,
Map
<
String
,
?>
params
)
{
String
joined
=
joinParamsForQuery
(
params
);
if
(!
joined
.
isEmpty
())
return
query
+
"?"
+
joined
;
...
...
src/main/java/kz/arta/synergy/api/asforms/AsFormProvider.java
View file @
81f08031
package
kz.arta.synergy.api.asforms
;
import
kz.arta.synergy.api.JsonUtils
;
import
kz.arta.synergy.api.Query
;
import
kz.arta.synergy.api.QueryContext
;
import
kz.arta.synergy.api.RestHttpQuery
;
import
kz.arta.synergy.api.asforms.annotations.*
;
import
kz.arta.synergy.api.asforms.exceptions.CreateAsFormException
;
import
kz.arta.synergy.api.asforms.exceptions.UnsupportedFieldTypeException
;
import
kz.arta.synergy.api.asforms.pojo.*
;
import
org.codehaus.jackson.type.TypeReference
;
import
java.io.IOException
;
import
java.lang.annotation.Annotation
;
...
...
@@ -31,12 +33,19 @@ public class AsFormProvider {
return
new
AsFormProvider
(
context
);
}
public
List
<
AdvancedSearchResult
>
advancedSearch
(
AdvancedSearchParams
advancedSearchParams
)
{
return
new
ArrayList
<
AdvancedSearchResult
>();
public
List
<
AdvancedSearchResult
>
advancedSearch
(
AdvancedSearchParams
searchParams
)
throws
IOException
{
Query
query
=
Query
.
newInstance
()
.
methodPost
()
.
header
(
"Content-Type"
,
"application/json;charset=utf-8"
)
.
url
(
"/rest/api/asforms/search/advanced"
)
.
body
(
JsonUtils
.
toJson
(
searchParams
));
String
result
=
restHttpQuery
.
doQuery
(
query
);
return
JsonUtils
.
read
(
result
,
new
TypeReference
<
List
<
AdvancedSearchResult
>>()
{});
}
public
<
T
extends
AsForm
>
T
fetch
(
Class
<
T
>
formClass
,
String
dataUUID
)
throws
IOException
{
String
data
=
restHttpQuery
.
do
GetQuery
(
"/rest/api/asforms/data/"
+
dataUUID
);
String
data
=
restHttpQuery
.
do
Query
(
Query
.
newInstance
().
url
(
"/rest/api/asforms/data/"
+
dataUUID
)
);
return
create
(
formClass
,
JsonUtils
.
read
(
data
,
AsFormWrapper
.
class
));
}
...
...
@@ -51,8 +60,15 @@ public class AsFormProvider {
public
<
T
extends
AsForm
>
String
save
(
T
asForm
)
throws
IOException
{
AsFormWrapper
asFormWrapper
=
toAsfData
(
asForm
);
JsonUtils
.
toJson
(
asFormWrapper
);
return
null
;
Query
query
=
Query
.
newInstance
()
.
methodPost
()
.
url
(
"/rest/api/asforms/data/save"
)
.
formParam
(
"formUUID"
,
asForm
.
getForm
())
.
formParam
(
"uuid"
,
asForm
.
getUuid
())
.
formParam
(
"data=\"data\":"
,
JsonUtils
.
toJson
(
asFormWrapper
));
return
restHttpQuery
.
doQuery
(
query
);
}
public
<
T
extends
AsForm
>
AsFormWrapper
toAsfData
(
T
asForm
)
{
...
...
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