• R/O
  • SSH
  • HTTPS

util: Commit


Commit MetaInfo

Revision180 (tree)
Time2019-04-08 19:55:27
Authorhirukawa_ryo

Log Message

* rest-client-util 0.2.1
依存ライブラリ jackson-datetime-moduleのバージョンを0.1.1→0.1.2に変更しました。
Responseオブジェクトをtry-with-resources構文で扱うように変更しました。

Change Summary

Incremental Difference

--- rest-client-util/trunk/src/main/java/net/osdn/util/rest/client/Server.java (revision 179)
+++ rest-client-util/trunk/src/main/java/net/osdn/util/rest/client/Server.java (revision 180)
@@ -735,25 +735,25 @@
735735
736736 public String get() throws IOException, HttpException {
737737 Request request = createRequest();
738- Response response = getClient().newCall(request).execute();
739-
740- if(!response.isSuccessful()) {
741- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
738+ try(Response response = getClient().newCall(request).execute()) {
739+ if(!response.isSuccessful()) {
740+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
741+ }
742+
743+ String value = response.body().string();
744+ return value;
742745 }
743-
744- String value = response.body().string();
745- return value;
746746 }
747747
748748 public String getString() throws IOException, HttpException {
749749 Request request = createRequest();
750- Response response = getClient().newCall(request).execute();
751-
752- if(!response.isSuccessful()) {
753- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
750+ try(Response response = getClient().newCall(request).execute()) {
751+ if(!response.isSuccessful()) {
752+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
753+ }
754+
755+ return Server.getString(response);
754756 }
755-
756- return Server.getString(response);
757757 }
758758
759759 public String getString(String defaultValue) throws IOException, HttpException {
@@ -766,13 +766,13 @@
766766
767767 public Boolean getBoolean() throws IOException, HttpException {
768768 Request request = createRequest();
769- Response response = getClient().newCall(request).execute();
770-
771- if(!response.isSuccessful()) {
772- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
769+ try(Response response = getClient().newCall(request).execute()) {
770+ if(!response.isSuccessful()) {
771+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
772+ }
773+
774+ return Server.getBoolean(response);
773775 }
774-
775- return Server.getBoolean(response);
776776 }
777777
778778 public boolean getBoolean(boolean defaultValue) throws IOException, HttpException {
@@ -785,13 +785,13 @@
785785
786786 public Integer getInt() throws IOException, HttpException {
787787 Request request = createRequest();
788- Response response = getClient().newCall(request).execute();
789-
790- if(!response.isSuccessful()) {
791- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
788+ try(Response response = getClient().newCall(request).execute()) {
789+ if(!response.isSuccessful()) {
790+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
791+ }
792+
793+ return Server.getInteger(response);
792794 }
793-
794- return Server.getInteger(response);
795795 }
796796
797797 public int getInt(int defaultValue) throws IOException, HttpException {
@@ -804,13 +804,13 @@
804804
805805 public Long getLong() throws IOException, HttpException {
806806 Request request = createRequest();
807- Response response = getClient().newCall(request).execute();
808-
809- if(!response.isSuccessful()) {
810- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
807+ try(Response response = getClient().newCall(request).execute()) {
808+ if(!response.isSuccessful()) {
809+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
810+ }
811+
812+ return Server.getLong(response);
811813 }
812-
813- return Server.getLong(response);
814814 }
815815
816816 public long getLong(long defaultValue) throws IOException, HttpException {
@@ -823,13 +823,13 @@
823823
824824 public Float getFloat() throws IOException, HttpException {
825825 Request request = createRequest();
826- Response response = getClient().newCall(request).execute();
827-
828- if(!response.isSuccessful()) {
829- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
826+ try(Response response = getClient().newCall(request).execute()) {
827+ if(!response.isSuccessful()) {
828+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
829+ }
830+
831+ return Server.getFloat(response);
830832 }
831-
832- return Server.getFloat(response);
833833 }
834834
835835 public float getFloat(float defaultValue) throws IOException, HttpException {
@@ -842,13 +842,13 @@
842842
843843 public Double getDouble() throws IOException, HttpException {
844844 Request request = createRequest();
845- Response response = getClient().newCall(request).execute();
846-
847- if(!response.isSuccessful()) {
848- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
845+ try(Response response = getClient().newCall(request).execute()) {
846+ if(!response.isSuccessful()) {
847+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
848+ }
849+
850+ return Server.getDouble(response);
849851 }
850-
851- return Server.getDouble(response);
852852 }
853853
854854 public double getDouble(double defaultValue) throws IOException, HttpException {
@@ -861,69 +861,69 @@
861861
862862 public <T> T get(Class<T> returnType) throws IOException, HttpException {
863863 Request request = createRequest();
864- Response response = getClient().newCall(request).execute();
865-
866- if(!response.isSuccessful()) {
867- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
864+ try(Response response = getClient().newCall(request).execute()) {
865+ if(!response.isSuccessful()) {
866+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
867+ }
868+
869+ T value = null;
870+
871+ InputStream in = response.body().byteStream();
872+ JsonParser parser = factory.createParser(in);
873+ JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnType, mapper, parser);
874+ if(iterator.hasNext()) {
875+ value = iterator.next();
876+ }
877+ return value;
868878 }
869-
870- T value = null;
871-
872- InputStream in = response.body().byteStream();
873- JsonParser parser = factory.createParser(in);
874- JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnType, mapper, parser);
875- if(iterator.hasNext()) {
876- value = iterator.next();
877- }
878- return value;
879879 }
880880
881881 public <T> List<T> getList(Class<T> returnClass) throws IOException, HttpException {
882882 Request request = createRequest();
883- Response response = getClient().newCall(request).execute();
884-
885- if(!response.isSuccessful()) {
886- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
883+ try(Response response = getClient().newCall(request).execute()) {
884+ if(!response.isSuccessful()) {
885+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
886+ }
887+
888+ InputStream in = response.body().byteStream();
889+ JsonParser parser = factory.createParser(in);
890+ JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser);
891+ List<T> list = new ArrayList<T>();
892+ while(iterator.hasNext()) {
893+ T item = iterator.next();
894+ list.add(item);
895+ }
896+ return list;
887897 }
888-
889- InputStream in = response.body().byteStream();
890- JsonParser parser = factory.createParser(in);
891- JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser);
892- List<T> list = new ArrayList<T>();
893- while(iterator.hasNext()) {
894- T item = iterator.next();
895- list.add(item);
896- }
897- return list;
898898 }
899899
900900 public <T> JsonObjectIterable<T> getIterable(Class<T> returnClass) throws IOException, HttpException {
901901 Request request = createRequest();
902- Response response = getClient().newCall(request).execute();
903-
904- if(!response.isSuccessful()) {
905- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
902+ try(Response response = getClient().newCall(request).execute()) {
903+ if(!response.isSuccessful()) {
904+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
905+ }
906+
907+ InputStream in = response.body().byteStream();
908+ JsonParser parser = factory.createParser(in);
909+ JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser, getRowCount(response));
910+ JsonObjectIterable<T> iterable = new JsonObjectIterable<T>(iterator);
911+ return iterable;
906912 }
907-
908- InputStream in = response.body().byteStream();
909- JsonParser parser = factory.createParser(in);
910- JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser, getRowCount(response));
911- JsonObjectIterable<T> iterable = new JsonObjectIterable<T>(iterator);
912- return iterable;
913913 }
914914
915915 public <T> JsonObjectIterator<T> getIterator(Class<T> returnClass) throws IOException, HttpException {
916916 Request request = createRequest();
917- Response response = getClient().newCall(request).execute();
918-
919- if(!response.isSuccessful()) {
920- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
917+ try(Response response = getClient().newCall(request).execute()) {
918+ if(!response.isSuccessful()) {
919+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
920+ }
921+
922+ InputStream in = response.body().byteStream();
923+ JsonParser parser = factory.createParser(in);
924+ JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser, getRowCount(response));
925+ return iterator;
921926 }
922-
923- InputStream in = response.body().byteStream();
924- JsonParser parser = factory.createParser(in);
925- JsonObjectIterator<T> iterator = new JsonObjectIterator<T>(returnClass, mapper, parser, getRowCount(response));
926- return iterator;
927927 }
928928
929929 /*
@@ -933,77 +933,77 @@
933933 */
934934 public Map<String, String> getMap() throws IOException, HttpException {
935935 Request request = createRequest();
936- Response response = getClient().newCall(request).execute();
937-
938- if(!response.isSuccessful()) {
939- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
936+ try(Response response = getClient().newCall(request).execute()) {
937+ if(!response.isSuccessful()) {
938+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
939+ }
940+
941+ Map<String, String> value = null;
942+
943+ InputStream in = response.body().byteStream();
944+ JsonParser parser = factory.createParser(in);
945+
946+ @SuppressWarnings({ "rawtypes", "unchecked" })
947+ JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser);
948+ if(iterator.hasNext()) {
949+ value = iterator.next();
950+ }
951+ return value;
940952 }
941-
942- Map<String, String> value = null;
943-
944- InputStream in = response.body().byteStream();
945- JsonParser parser = factory.createParser(in);
946-
947- @SuppressWarnings({ "rawtypes", "unchecked" })
948- JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser);
949- if(iterator.hasNext()) {
950- value = iterator.next();
951- }
952- return value;
953953 }
954954
955955 public List<Map<String, String>> getMapList() throws IOException, HttpException {
956956 Request request = createRequest();
957- Response response = getClient().newCall(request).execute();
958-
959- if(!response.isSuccessful()) {
960- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
957+ try(Response response = getClient().newCall(request).execute()) {
958+ if(!response.isSuccessful()) {
959+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
960+ }
961+
962+ InputStream in = response.body().byteStream();
963+ JsonParser parser = factory.createParser(in);
964+
965+ @SuppressWarnings({ "rawtypes", "unchecked" })
966+ JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser);
967+ List<Map<String, String>> list = new ArrayList<Map<String, String>>();
968+ while(iterator.hasNext()) {
969+ Map<String, String> item = iterator.next();
970+ list.add(item);
971+ }
972+ return list;
961973 }
962-
963- InputStream in = response.body().byteStream();
964- JsonParser parser = factory.createParser(in);
965-
966- @SuppressWarnings({ "rawtypes", "unchecked" })
967- JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser);
968- List<Map<String, String>> list = new ArrayList<Map<String, String>>();
969- while(iterator.hasNext()) {
970- Map<String, String> item = iterator.next();
971- list.add(item);
972- }
973- return list;
974974 }
975975
976976 public JsonObjectIterable<Map<String, String>> getMapIterable() throws IOException, HttpException {
977977 Request request = createRequest();
978- Response response = getClient().newCall(request).execute();
979-
980- if(!response.isSuccessful()) {
981- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
978+ try(Response response = getClient().newCall(request).execute()) {
979+ if(!response.isSuccessful()) {
980+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
981+ }
982+
983+ InputStream in = response.body().byteStream();
984+ JsonParser parser = factory.createParser(in);
985+
986+ @SuppressWarnings({ "rawtypes", "unchecked" })
987+ JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser, getRowCount(response));
988+ JsonObjectIterable<Map<String, String>> iterable = new JsonObjectIterable<Map<String, String>>(iterator);
989+ return iterable;
982990 }
983-
984- InputStream in = response.body().byteStream();
985- JsonParser parser = factory.createParser(in);
986-
987- @SuppressWarnings({ "rawtypes", "unchecked" })
988- JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser, getRowCount(response));
989- JsonObjectIterable<Map<String, String>> iterable = new JsonObjectIterable<Map<String, String>>(iterator);
990- return iterable;
991991 }
992992
993993 public JsonObjectIterator<Map<String, String>> getMapIterator() throws IOException, HttpException {
994994 Request request = createRequest();
995- Response response = getClient().newCall(request).execute();
996-
997- if(!response.isSuccessful()) {
998- throw HttpException.byStatus(response.code(), response.message(), response.body().string());
995+ try(Response response = getClient().newCall(request).execute()) {
996+ if(!response.isSuccessful()) {
997+ throw HttpException.byStatus(response.code(), response.message(), response.body().string());
998+ }
999+
1000+ InputStream in = response.body().byteStream();
1001+ JsonParser parser = factory.createParser(in);
1002+
1003+ @SuppressWarnings({ "rawtypes", "unchecked" })
1004+ JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser, getRowCount(response));
1005+ return iterator;
9991006 }
1000-
1001- InputStream in = response.body().byteStream();
1002- JsonParser parser = factory.createParser(in);
1003-
1004- @SuppressWarnings({ "rawtypes", "unchecked" })
1005- JsonObjectIterator<Map<String, String>> iterator = new JsonObjectIterator(LinkedHashMap.class, mapper, parser, getRowCount(response));
1006- return iterator;
10071007 }
10081008 }
10091009
@@ -1020,7 +1020,7 @@
10201020 @Override
10211021 public Request authenticate(Route route, Response response) throws IOException {
10221022 String credential = Credentials.basic(username, password);
1023- return response.request().newBuilder().header("Authorization", credential).build();
1023+ return response.request().newBuilder().header("Authorization", credential).build();
10241024 }
10251025 }
10261026 }
Show on old repository browser