25
25
import java .net .URL ;
26
26
import java .net .URLEncoder ;
27
27
import java .nio .charset .Charset ;
28
+ import java .nio .charset .StandardCharsets ;
29
+ import java .nio .file .Paths ;
28
30
import java .util .Date ;
29
31
import java .util .List ;
30
32
import java .util .Map ;
31
33
import java .util .UUID ;
32
34
33
35
public class JsonServiceClient implements ServiceClient {
34
- static Charset UTF8 = Charset . forName ( "UTF-8" ) ;
36
+ static Charset UTF8 = StandardCharsets . UTF_8 ;
35
37
String baseUrl ;
36
38
URI baseUri ;
37
39
String replyUrl ;
40
+ String oneWayUrl ;
38
41
39
42
boolean alwaysSendBasicAuthHeaders ;
40
43
String userName ;
@@ -56,10 +59,11 @@ public JsonServiceClient(String baseUrl) {
56
59
this (baseUrl , true );
57
60
}
58
61
public JsonServiceClient (String baseUrl , boolean initCookies ) {
59
- setBaseUrl (baseUrl );
60
62
if (initCookies ) {
61
63
initCookieHandler ();
62
64
}
65
+ setBaseUrl (baseUrl );
66
+ setBasePath ("api" );
63
67
}
64
68
65
69
public void initCookieHandler () {
@@ -76,24 +80,41 @@ public void setBaseUrl(String baseUrl) {
76
80
} catch (URISyntaxException e ) {
77
81
throw new RuntimeException (e );
78
82
}
79
- this .replyUrl = this .baseUrl + "json/reply/" ;
80
83
}
81
84
85
+ public JsonServiceClient setBasePath () { return setBasePath ("" ); }
86
+ public JsonServiceClient setBasePath (String basePath ) {
87
+ if (basePath == null || basePath .length () == 0 ) {
88
+ this .replyUrl = this .baseUri .resolve ("json/reply" ) + "/" ;
89
+ this .oneWayUrl = this .baseUri .resolve ("json/oneway" ) + "/" ;
90
+ } else {
91
+ if (basePath .endsWith ("/" )) {
92
+ basePath = basePath .substring (0 , basePath .length () -1 );
93
+ }
94
+ this .replyUrl = this .baseUri .resolve (basePath ) + "/" ;
95
+ this .oneWayUrl = this .baseUri .resolve (basePath ) + "/" ;
96
+ }
97
+ return this ;
98
+ }
99
+
100
+ public String getBaseUrl () { return this .baseUrl ; }
101
+ public String getReplyUrl () { return this .replyUrl ; }
102
+
82
103
public void setTimeout (int timeoutMs ) {
83
104
this .timeoutMs = timeoutMs ;
84
105
}
85
106
86
107
public GsonBuilder getGsonBuilder () {
87
108
return new GsonBuilder ()
88
- .registerTypeAdapterFactory (JsonSerializers .getCaseInsensitiveEnumTypeAdapterFactory ())
89
- .registerTypeAdapter (Date .class , JsonSerializers .getDateSerializer ())
90
- .registerTypeAdapter (Date .class , JsonSerializers .getDateDeserializer ())
91
- .registerTypeAdapter (TimeSpan .class , JsonSerializers .getTimeSpanSerializer ())
92
- .registerTypeAdapter (TimeSpan .class , JsonSerializers .getTimeSpanDeserializer ())
93
- .registerTypeAdapter (UUID .class , JsonSerializers .getGuidSerializer ())
94
- .registerTypeAdapter (UUID .class , JsonSerializers .getGuidDeserializer ())
95
- .registerTypeAdapter (byte [].class , JsonSerializers .getByteArraySerializer ())
96
- .registerTypeAdapter (byte [].class , JsonSerializers .getByteArrayDeserializer ());
109
+ .registerTypeAdapterFactory (JsonSerializers .getCaseInsensitiveEnumTypeAdapterFactory ())
110
+ .registerTypeAdapter (Date .class , JsonSerializers .getDateSerializer ())
111
+ .registerTypeAdapter (Date .class , JsonSerializers .getDateDeserializer ())
112
+ .registerTypeAdapter (TimeSpan .class , JsonSerializers .getTimeSpanSerializer ())
113
+ .registerTypeAdapter (TimeSpan .class , JsonSerializers .getTimeSpanDeserializer ())
114
+ .registerTypeAdapter (UUID .class , JsonSerializers .getGuidSerializer ())
115
+ .registerTypeAdapter (UUID .class , JsonSerializers .getGuidDeserializer ())
116
+ .registerTypeAdapter (byte [].class , JsonSerializers .getByteArraySerializer ())
117
+ .registerTypeAdapter (byte [].class , JsonSerializers .getByteArrayDeserializer ());
97
118
}
98
119
99
120
public Gson getGson () {
@@ -103,23 +124,23 @@ public Gson getGson() {
103
124
return gson ;
104
125
}
105
126
106
- public String toJson (Object o ){
127
+ public String toJson (Object o ) {
107
128
String json = getGson ().toJson (o );
108
129
return json ;
109
130
}
110
131
111
- public Object fromJson (String json , Class c ){
132
+ public Object fromJson (String json , Class c ) {
112
133
Object o = getGson ().fromJson (json , c );
113
134
return o ;
114
135
}
115
136
116
137
public void setGson (Gson gson ) { this .gson = gson ; }
117
138
118
- public String createUrl (Object requestDto ){
139
+ public String createUrl (Object requestDto ) {
119
140
return createUrl (requestDto , null );
120
141
}
121
142
122
- public String createUrl (Object requestDto , Map <String ,String > query ){
143
+ public String createUrl (Object requestDto , Map <String ,String > query ) {
123
144
String requestUrl = this .replyUrl + requestDto .getClass ().getSimpleName ();
124
145
125
146
StringBuilder sb = new StringBuilder ();
0 commit comments