4848 */
4949public class MALClient implements AutoCloseable {
5050
51- private final Client client ;
51+ private Client client ;
52+
53+ private final ClientConfig clientConfig ;
5254 private final String malUrl ;
5355 private final String username ;
5456
@@ -75,13 +77,9 @@ public MALClient(
7577 @ NonNull final String malUrl ) {
7678
7779 this .username = username ;
78- this .client = ClientBuilder .newClient (
79- new ClientConfig ()
80- .connectorProvider (new ApacheConnectorProvider ())
81- .register (HttpAuthenticationFeature .basicBuilder ()
82- .credentials (username , password )
83- .build ()));
8480 this .malUrl = malUrl ;
81+ this .clientConfig = createClientConfig (username ,password );
82+ this .client = ClientBuilder .newClient (clientConfig );
8583 }
8684
8785 /**
@@ -642,6 +640,11 @@ private void handleError(Response response){
642640 int status = response .getStatus ();
643641 String message = response .readEntity (String .class );
644642
643+ if (status == Response .Status .NO_CONTENT .getStatusCode ()){
644+ // This is necessary because Jersey cannot handle MAL's 204 response correctly for whatever reason.
645+ this .client .close ();
646+ this .client = ClientBuilder .newClient (this .clientConfig );
647+ }
645648 if (status == Response .Status .UNAUTHORIZED .getStatusCode ()){
646649 throw new NotAuthorizedException (message );
647650 }
@@ -653,6 +656,13 @@ private void handleError(Response response){
653656 }
654657 }
655658
659+ private ClientConfig createClientConfig (String username , String password ){
660+ ClientConfig clientConfig = new ClientConfig ();
661+ clientConfig .connectorProvider (new ApacheConnectorProvider ());
662+ clientConfig .register (HttpAuthenticationFeature .basicBuilder ().credentials (username ,password ).build ());
663+ return clientConfig ;
664+ }
665+
656666 @ Override
657667 public void close () {
658668 if (client != null ) {
0 commit comments