1717package org .datatransferproject .datatransfer .apple .signals ;
1818
1919import com .fasterxml .jackson .databind .ObjectMapper ;
20- import com .google .common .annotations .VisibleForTesting ;
2120import java .io .IOException ;
22- import java .io .Serializable ;
2321import java .net .HttpURLConnection ;
2422import java .net .URL ;
2523import java .nio .charset .StandardCharsets ;
26- import java .util .Map ;
2724import java .util .UUID ;
2825import org .apache .commons .io .IOUtils ;
29- import org .apache .commons .lang3 .SerializationUtils ;
3026import org .datatransferproject .api .launcher .Monitor ;
3127import org .datatransferproject .datatransfer .apple .AppleBaseInterface ;
32- import org .datatransferproject .datatransfer .apple .AppleInterfaceFactory ;
3328import org .datatransferproject .datatransfer .apple .constants .AuditKeys ;
3429import org .datatransferproject .datatransfer .apple .constants .Headers ;
3530import org .datatransferproject .spi .transfer .provider .SignalRequest ;
36- import org .datatransferproject .spi .transfer .types .CopyException ;
3731import org .datatransferproject .spi .transfer .types .CopyExceptionWithFailureReason ;
3832import org .datatransferproject .spi .transfer .types .PermissionDeniedException ;
3933import org .datatransferproject .spi .transfer .types .UnconfirmedUserException ;
4034import org .datatransferproject .transfer .JobMetadata ;
4135import org .datatransferproject .types .transfer .auth .AppCredentials ;
4236import org .datatransferproject .types .transfer .auth .TokensAndUrlAuthData ;
43- import org .datatransferproject .types .transfer .retry .RetryStrategyLibrary ;
4437import org .jetbrains .annotations .NotNull ;
4538
4639/**
4740 * An Interface to send the Transfer Signals to Apple.
4841 */
4942public class AppleSignalInterface implements AppleBaseInterface {
5043 private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
44+ private static final String MEDIA_TYPE_JSON = "application/json" ;
45+ private static final String MEDIA_TYPE_FORM = "application/x-www-form-urlencoded" ;
5146
5247 protected String baseUrl ;
5348 protected AppCredentials appCredentials ;
@@ -89,8 +84,12 @@ public String sendPostRequest(@NotNull String url, @NotNull final byte[] request
8984 con .setRequestMethod ("POST" );
9085 con .setRequestProperty (Headers .AUTHORIZATION .getValue (), authData .getAccessToken ());
9186 con .setRequestProperty (Headers .CORRELATION_ID .getValue (), correlationId );
92- con .setRequestProperty (Headers .CONTENT_TYPE .getValue (), "application/json" );
93- con .setRequestProperty (Headers .ACCEPT .getValue (), "application/json" );
87+ con .setRequestProperty (Headers .ACCEPT .getValue (), MEDIA_TYPE_JSON );
88+ if (url .equals (authData .getTokenServerEncodedUrl ())) {
89+ con .setRequestProperty (Headers .CONTENT_TYPE .getValue (), MEDIA_TYPE_FORM );
90+ } else {
91+ con .setRequestProperty (Headers .CONTENT_TYPE .getValue (), MEDIA_TYPE_JSON );
92+ }
9493
9594 IOUtils .write (requestData , con .getOutputStream ());
9695 responseString = IOUtils .toString (con .getInputStream (), StandardCharsets .ISO_8859_1 );
@@ -106,15 +105,17 @@ public String sendPostRequest(@NotNull String url, @NotNull final byte[] request
106105 AuditKeys .error ,
107106 e .getMessage (),
108107 AuditKeys .errorCode ,
109- con .getResponseCode (),
108+ ( con != null ? con .getResponseCode () : - 1 ),
110109 e );
111110 convertAndThrowException (e , con );
112111 } finally {
113- con .disconnect ();
112+ if (con != null ) {
113+ con .disconnect ();
114+ }
114115 }
115- return responseString ;
116116 }
117- return null ;
117+
118+ return responseString ;
118119 }
119120
120121 public byte [] sendSignal (@ NotNull final SignalRequest signalRequest )
0 commit comments