13
13
import com .ning .http .client .Request ;
14
14
import com .ning .http .client .RequestBuilder ;
15
15
import com .ning .http .client .Response ;
16
+
16
17
import org .slf4j .Logger ;
17
18
import org .slf4j .LoggerFactory ;
18
19
import org .zendesk .client .v2 .model .Attachment ;
@@ -665,6 +666,14 @@ private String json(Object object) {
665
666
666
667
private <T > ListenableFuture <T > submit (Request request , AsyncCompletionHandler <T > handler ) {
667
668
try {
669
+ if (request .getStringData () != null ) {
670
+ logger .debug ("Request {} {}\n {}" , request .getMethod (), request .getUrl (), request .getStringData ());
671
+ } else if (request .getByteData () != null ) {
672
+ logger .debug ("Request {} {} {} {} bytes" , request .getMethod (), request .getUrl (), //
673
+ request .getHeaders ().getFirstValue ("Content-type" ), request .getByteData ().length );
674
+ } else {
675
+ logger .debug ("Request {} {}" , request .getMethod (), request .getUrl ());
676
+ }
668
677
return client .executeRequest (request , handler );
669
678
} catch (IOException e ) {
670
679
throw new ZenDeskException (e .getMessage (), e );
@@ -716,12 +725,11 @@ protected AsyncCompletionHandler<Void> handleStatus() {
716
725
return new AsyncCompletionHandler <Void >() {
717
726
@ Override
718
727
public Void onCompleted (Response response ) throws Exception {
719
- logger .debug ("Response HTTP/{} {}\n {}" , response .getStatusCode (), response .getStatusText (),
720
- response .getResponseBody ());
721
- if (response .getStatusCode () / 100 == 2 ) {
728
+ logResponse (response );
729
+ if (isStatus2xx (response )) {
722
730
return null ;
723
731
}
724
- throw new ZenDeskException (response . getStatusText () );
732
+ throw new ZenDeskResponseException (response );
725
733
}
726
734
};
727
735
}
@@ -730,15 +738,14 @@ protected <T> AsyncCompletionHandler<T> handle(final Class<T> clazz) {
730
738
return new AsyncCompletionHandler <T >() {
731
739
@ Override
732
740
public T onCompleted (Response response ) throws Exception {
733
- logger .debug ("Response HTTP/{} {}\n {}" , response .getStatusCode (), response .getStatusText (),
734
- response .getResponseBody ());
735
- if (response .getStatusCode () / 100 == 2 ) {
741
+ logResponse (response );
742
+ if (isStatus2xx (response )) {
736
743
return (T ) mapper .reader (clazz ).readValue (response .getResponseBodyAsBytes ());
737
744
}
738
745
if (response .getStatusCode () == 404 ) {
739
746
return null ;
740
747
}
741
- throw new ZenDeskException (response . getStatusText () );
748
+ throw new ZenDeskResponseException (response );
742
749
}
743
750
};
744
751
}
@@ -747,15 +754,14 @@ protected <T> AsyncCompletionHandler<T> handle(final Class<T> clazz, final Strin
747
754
return new AsyncCompletionHandler <T >() {
748
755
@ Override
749
756
public T onCompleted (Response response ) throws Exception {
750
- logger .debug ("Response HTTP/{} {}\n {}" , response .getStatusCode (), response .getStatusText (),
751
- response .getResponseBody ());
752
- if (response .getStatusCode () / 100 == 2 ) {
757
+ logResponse (response );
758
+ if (isStatus2xx (response )) {
753
759
return mapper .convertValue (mapper .readTree (response .getResponseBodyAsBytes ()).get (name ), clazz );
754
760
}
755
761
if (response .getStatusCode () == 404 ) {
756
762
return null ;
757
763
}
758
- throw new ZenDeskException (response . getStatusText () );
764
+ throw new ZenDeskResponseException (response );
759
765
}
760
766
};
761
767
}
@@ -764,16 +770,15 @@ protected <T> AsyncCompletionHandler<List<T>> handleList(final Class<T> clazz) {
764
770
return new AsyncCompletionHandler <List <T >>() {
765
771
@ Override
766
772
public List <T > onCompleted (Response response ) throws Exception {
767
- logger .info ("Response HTTP/{} {}\n {}" , response .getStatusCode (), response .getStatusText (),
768
- response .getResponseBody ());
769
- if (response .getStatusCode () / 100 == 2 ) {
773
+ logResponse (response );
774
+ if (isStatus2xx (response )) {
770
775
List <T > values = new ArrayList <T >();
771
776
for (JsonNode node : mapper .readTree (response .getResponseBodyAsBytes ())) {
772
777
values .add (mapper .convertValue (node , clazz ));
773
778
}
774
779
return values ;
775
780
}
776
- throw new ZenDeskException (response . getStatusText () );
781
+ throw new ZenDeskResponseException (response );
777
782
}
778
783
};
779
784
}
@@ -782,16 +787,15 @@ protected <T> AsyncCompletionHandler<List<T>> handleList(final Class<T> clazz, f
782
787
return new AsyncCompletionHandler <List <T >>() {
783
788
@ Override
784
789
public List <T > onCompleted (Response response ) throws Exception {
785
- logger .debug ("Response HTTP/{} {}\n {}" , response .getStatusCode (), response .getStatusText (),
786
- response .getResponseBody ());
787
- if (response .getStatusCode () / 100 == 2 ) {
790
+ logResponse (response );
791
+ if (isStatus2xx (response )) {
788
792
List <T > values = new ArrayList <T >();
789
793
for (JsonNode node : mapper .readTree (response .getResponseBodyAsBytes ()).get (name )) {
790
794
values .add (mapper .convertValue (node , clazz ));
791
795
}
792
796
return values ;
793
797
}
794
- throw new ZenDeskException (response . getStatusText () );
798
+ throw new ZenDeskResponseException (response );
795
799
}
796
800
};
797
801
}
@@ -804,6 +808,18 @@ private Uri cnst(String template) {
804
808
return new FixedUri (url + template );
805
809
}
806
810
811
+ private void logResponse (Response response ) throws IOException {
812
+ logger .debug ("Response HTTP/{} {}\n {}" , response .getStatusCode (), response .getStatusText (),
813
+ response .getResponseBody ());
814
+ if (logger .isTraceEnabled ()) {
815
+ logger .trace ("Response headers {}" , response .getHeaders ());
816
+ }
817
+ }
818
+
819
+ private boolean isStatus2xx (Response response ) {
820
+ return response .getStatusCode () / 100 == 2 ;
821
+ }
822
+
807
823
//////////////////////////////////////////////////////////////////////
808
824
// Static helper methods
809
825
//////////////////////////////////////////////////////////////////////
@@ -814,6 +830,9 @@ private static <T> T complete(ListenableFuture<T> future) {
814
830
} catch (InterruptedException e ) {
815
831
throw new ZenDeskException (e .getMessage (), e );
816
832
} catch (ExecutionException e ) {
833
+ if (e .getCause () instanceof ZenDeskException ) {
834
+ throw (ZenDeskException ) e .getCause ();
835
+ }
817
836
throw new ZenDeskException (e .getMessage (), e );
818
837
}
819
838
}
0 commit comments