2222import java .net .URL ;
2323import java .net .URLDecoder ;
2424import java .net .URLEncoder ;
25+ import java .util .Collections ;
2526import java .util .HashMap ;
2627import java .util .Map ;
2728
@@ -45,90 +46,111 @@ public HttpSequenceRangeGetter(String authorizeUrl, String clientId, String auth
4546 }
4647
4748 /**
48- * param: client_id=1&access_token=12
49- * return: begin_value=1&end_value=2
49+ * param: clientId= &accessToken= &schemaName= &tableName= &step=
50+ * return: beginValue= &endValue= &errorCode= &errorMessage=
5051 */
5152 @ Override
5253 public SequenceRange get (String schemaName , String tableName , int step ) throws Exception {
5354 Map <String , String > param = new HashMap <>();
54- param .put ("client_id " , clientId );
55- param .put ("access_token " , accessToken );
56- param .put ("schema_name " , schemaName );
57- param .put ("table_name " , tableName );
55+ param .put ("clientId " , clientId );
56+ param .put ("accessToken " , accessToken );
57+ param .put ("schemaName " , schemaName );
58+ param .put ("tableName " , tableName );
5859 param .put ("step" , String .valueOf (step ));
5960 String result = HttpUtils .sendPost (accessUrl , param );
60- String [] kvs = result .split ("&" );
61- Long beginValue = null ;
62- Long endValue = null ;
63- boolean hasErrorCode = false ;
64- for (String item : kvs ) {
65- String [] kv = item .split ("=" );
66- if ("error_code" .equals (kv [0 ])) {
67- hasErrorCode = true ;
68- break ;
69- } else if ("begin_value" .equals (kv [0 ])) {
70- beginValue = Long .valueOf (kv [1 ].trim ());
71- } else if ("end_value" .equals (kv [0 ])) {
72- endValue = Long .valueOf (kv [1 ].trim ());
73- }
74- }
75- if (hasErrorCode ) {
76- authorize ();
77- result = HttpUtils .sendPost (accessUrl , param );
78- kvs = result .split ("&" );
79- for (String item : kvs ) {
80- String [] kv = item .split ("=" );
81- if ("error_code" .equals (kv [0 ])) {
61+ Map <String , String > resultMap = parseHttpKvString (result );
62+ if (resultMap .isEmpty ()) {
63+ return null ;
64+ } else {
65+ if (resultMap .get ("errorCode" ) != null ) {
66+ authorize ();
67+ result = HttpUtils .sendPost (accessUrl , param );
68+ resultMap = parseHttpKvString (result );
69+ if (resultMap .get ("errorCode" ) != null ) {
8270 throw new GetSequenceFailedException ("clientId:" + clientId
8371 + " access data failed, return message is:" + result );
84- } else if ("begin_value" .equals (kv [0 ])) {
85- beginValue = Long .valueOf (kv [1 ].trim ());
86- } else if ("end_value" .equals (kv [0 ])) {
87- endValue = Long .valueOf (kv [1 ].trim ());
8872 }
8973 }
74+ SequenceRange sequenceRange = new SequenceRange ();
75+ sequenceRange .setBeginValue (parseLong (resultMap .get ("beginValue" )));
76+ sequenceRange .setEndValue (parseLong (resultMap .get ("endValue" )));
77+ return sequenceRange ;
9078 }
91- SequenceRange sequenceRange = new SequenceRange ();
92- sequenceRange .setBeginValue (beginValue );
93- sequenceRange .setEndValue (endValue );
94- return sequenceRange ;
9579 }
9680
9781 /**
98- * param: client_id=&client_token =
99- * return: access_url=http&access_token=1&error_code=1
82+ * param: clientId= &clientToken =
83+ * return: accessUrl= &accessToken= &errorCode= &errorMessage=
10084 */
10185 private void authorize () {
10286 Map <String , String > param = new HashMap <>();
103- param .put ("client_id " , clientId );
104- param .put ("authorize_token " , authorizeToken );
87+ param .put ("clientId " , clientId );
88+ param .put ("authorizeToken " , authorizeToken );
10589 String result = HttpUtils .sendPost (authorizeUrl , param );
106- String [] kvs = result .split ("&" );
107- for (String item : kvs ) {
108- String [] kv = item .split ("=" );
109- if ("error_code" .equals (kv [0 ]) && kv .length >= 2 && kv [1 ] != null && kv [1 ].trim ().length () > 0 ) {
90+ Map <String , String > resultMap = parseHttpKvString (result );
91+ if (resultMap .isEmpty ()) {
92+ throw new GetSequenceFailedException ("clientId:" + clientId + " authorize failed, return message is empty" );
93+ } else {
94+ if (resultMap .get ("errorCode" ) != null ) {
11095 throw new GetSequenceFailedException ("clientId:" + clientId + " authorize failed, return message is: "
11196 + result );
112- } else if ("access_url" .equals (kv [0 ])) {
113- this .accessUrl = kv [1 ];
114- } else if ("access_token" .equals (kv [0 ])) {
115- this .accessToken = kv [1 ];
11697 }
117- }
118- if (accessUrl != null ) {
98+ String accessUrl = resultMap .get ("accessUrl" );
99+ String accessToken = resultMap .get ("accessToken" );
100+ if (accessUrl == null || accessToken == null ) {
101+ throw new GetSequenceFailedException (
102+ "clientId:"
103+ + clientId
104+ + " authorize failed, accessUrl or accessToken is null, detail message is "
105+ + result );
106+ }
119107 try {
120108 accessUrl = URLDecoder .decode (accessUrl .trim (), "UTF-8" );
121109 } catch (UnsupportedEncodingException e ) {
122- throw new RuntimeException (e );
110+ throw new GetSequenceFailedException (e );
111+ }
112+ this .accessUrl = accessUrl ;
113+ this .accessToken = accessToken ;
114+ }
115+ }
116+
117+ private Map <String , String > parseHttpKvString (String string ) {
118+ if (string == null ) {
119+ return Collections .emptyMap ();
120+ }
121+ string = string .trim ();
122+ if (string .length () == 0 ) {
123+ return Collections .emptyMap ();
124+ }
125+ Map <String , String > map = new HashMap <>();
126+ String [] kvs = string .split ("&" );
127+ for (String kvString : kvs ) {
128+ String [] kv = kvString .split ("=" );
129+ if (kv .length == 2 ) {
130+ String val = kv [1 ];
131+ val = val .trim ();
132+ if (val .length () == 0 ) {
133+ val = null ;
134+ }
135+ map .put (kv [0 ], val );
136+ } else if (kv .length == 1 ) {
137+ map .put (kv [0 ], null );
138+ } else {
139+ throw new RuntimeException ("Illegaled http kv string:" + kvString );
123140 }
124141 }
125- if (accessToken != null ) {
126- accessToken = accessToken .trim ();
142+ return map ;
143+ }
144+
145+ private Long parseLong (String str ) {
146+ if (str == null ) {
147+ return null ;
127148 }
128- if ( accessUrl == null || accessUrl . length () == 0 || accessToken == null || accessToken . length () == 0 ) {
129- throw new GetSequenceFailedException ( "clientId:" + clientId + " authorize failed, return message is: "
130- + result ) ;
149+ str = str . trim ();
150+ if ( str . length () == 0 ) {
151+ return null ;
131152 }
153+ return Long .parseLong (str );
132154 }
133155
134156 static class HttpUtils {
@@ -180,7 +202,8 @@ public static String sendPost(String url, Map<String, String> params) {
180202 }
181203 int responseCode = con .getResponseCode ();
182204 if (responseCode != 200 ) {
183- throw new IllegalStateException ("http code " + responseCode + "\n " + response .toString ());
205+ throw new IllegalStateException ("the requested url: " + url + " returned error code: "
206+ + responseCode + "\n " + response .toString ());
184207 } else {
185208 return response .toString ();
186209 }
0 commit comments