@@ -294,46 +294,50 @@ private FieldError validateIdpConnectivity(SamlSSOClientConfig samlConfig) {
294294
295295 URL url = new URL (urlWithParams );
296296 HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
297- conn .setRequestMethod ("GET" );
298- conn .setConnectTimeout (5000 );
299- conn .setReadTimeout (5000 );
300- conn .setInstanceFollowRedirects (false );
301- int responseCode = conn .getResponseCode ();
302- LOG .debug ("IdP response code to SAML request: {}" , responseCode );
303-
304- // Analyze response
305- if (responseCode == 404 ) {
306- return ValidationErrorBuilder .createFieldError (
307- ValidationErrorBuilder .FieldPaths .SAML_IDP_SSO_URL ,
308- "SSO Login URL not found (HTTP 404). The URL '" + ssoUrl + "' does not exist." );
309- } else if (responseCode == 405 ) {
310- return ValidationErrorBuilder .createFieldError (
311- ValidationErrorBuilder .FieldPaths .SAML_IDP_SSO_URL ,
312- "SSO URL doesn't accept GET requests (HTTP 405). Please check the SSO URL configuration." );
313- } else if (responseCode >= 200 && responseCode < 400 ) {
314- // 200 or 302 means the IdP accepted our SAML request
315- return null ; // Success - SSO Login URL validated
316- } else if (responseCode >= 400 && responseCode < 500 ) {
317- // 400-499 could mean wrong URL or IdP rejecting the request
318- // Read a bit of the response to check for specific errors
319- String responseSnippet = readResponseSnippet (conn );
320- if (responseSnippet .toLowerCase ().contains ("saml" )
321- || responseSnippet .toLowerCase ().contains ("invalid" )) {
322- // Warning case - treat as success
323- LOG .warn (
324- "SSO URL responded with client error (HTTP {}). This might be due to the test SAML request format." ,
325- responseCode );
326- return null ;
297+ try {
298+ conn .setRequestMethod ("GET" );
299+ conn .setConnectTimeout (5000 );
300+ conn .setReadTimeout (5000 );
301+ conn .setInstanceFollowRedirects (false );
302+ int responseCode = conn .getResponseCode ();
303+ LOG .debug ("IdP response code to SAML request: {}" , responseCode );
304+
305+ // Analyze response
306+ if (responseCode == 404 ) {
307+ return ValidationErrorBuilder .createFieldError (
308+ ValidationErrorBuilder .FieldPaths .SAML_IDP_SSO_URL ,
309+ "SSO Login URL not found (HTTP 404). The URL '" + ssoUrl + "' does not exist." );
310+ } else if (responseCode == 405 ) {
311+ return ValidationErrorBuilder .createFieldError (
312+ ValidationErrorBuilder .FieldPaths .SAML_IDP_SSO_URL ,
313+ "SSO URL doesn't accept GET requests (HTTP 405). Please check the SSO URL configuration." );
314+ } else if (responseCode >= 200 && responseCode < 400 ) {
315+ // 200 or 302 means the IdP accepted our SAML request
316+ return null ; // Success - SSO Login URL validated
317+ } else if (responseCode >= 400 && responseCode < 500 ) {
318+ // 400-499 could mean wrong URL or IdP rejecting the request
319+ // Read a bit of the response to check for specific errors
320+ String responseSnippet = readResponseSnippet (conn );
321+ if (responseSnippet .toLowerCase ().contains ("saml" )
322+ || responseSnippet .toLowerCase ().contains ("invalid" )) {
323+ // Warning case - treat as success
324+ LOG .warn (
325+ "SSO URL responded with client error (HTTP {}). This might be due to the test SAML request format." ,
326+ responseCode );
327+ return null ;
328+ }
329+ return ValidationErrorBuilder .createFieldError (
330+ ValidationErrorBuilder .FieldPaths .SAML_IDP_SSO_URL ,
331+ "SSO URL returned error (HTTP "
332+ + responseCode
333+ + "). Please verify the URL is correct." );
334+ } else {
335+ return ValidationErrorBuilder .createFieldError (
336+ ValidationErrorBuilder .FieldPaths .SAML_IDP_SSO_URL ,
337+ "SSO URL is not accessible (HTTP " + responseCode + ")" );
327338 }
328- return ValidationErrorBuilder .createFieldError (
329- ValidationErrorBuilder .FieldPaths .SAML_IDP_SSO_URL ,
330- "SSO URL returned error (HTTP "
331- + responseCode
332- + "). Please verify the URL is correct." );
333- } else {
334- return ValidationErrorBuilder .createFieldError (
335- ValidationErrorBuilder .FieldPaths .SAML_IDP_SSO_URL ,
336- "SSO URL is not accessible (HTTP " + responseCode + ")" );
339+ } finally {
340+ conn .disconnect ();
337341 }
338342 } catch (Exception e ) {
339343 LOG .warn ("SSO URL validation failed" , e );
@@ -376,10 +380,13 @@ private String createTestSamlRequest(SamlSSOClientConfig samlConfig) {
376380 java .io .ByteArrayOutputStream bytesOut = new java .io .ByteArrayOutputStream ();
377381 java .util .zip .Deflater deflater =
378382 new java .util .zip .Deflater (java .util .zip .Deflater .DEFLATED , true );
379- java .util .zip .DeflaterOutputStream deflaterStream =
380- new java .util .zip .DeflaterOutputStream (bytesOut , deflater );
381- deflaterStream .write (samlRequestXml .getBytes (StandardCharsets .UTF_8 ));
382- deflaterStream .finish ();
383+ try (java .util .zip .DeflaterOutputStream deflaterStream =
384+ new java .util .zip .DeflaterOutputStream (bytesOut , deflater )) {
385+ deflaterStream .write (samlRequestXml .getBytes (StandardCharsets .UTF_8 ));
386+ deflaterStream .finish ();
387+ } finally {
388+ deflater .end ();
389+ }
383390
384391 // Base64 encode
385392 String base64Request = Base64 .getEncoder ().encodeToString (bytesOut .toByteArray ());
@@ -400,10 +407,12 @@ private String readResponseSnippet(HttpURLConnection conn) {
400407 inputStream = conn .getInputStream ();
401408 }
402409 if (inputStream != null ) {
403- byte [] buffer = new byte [500 ];
404- int bytesRead = inputStream .read (buffer );
405- if (bytesRead > 0 ) {
406- return new String (buffer , 0 , bytesRead , StandardCharsets .UTF_8 );
410+ try (inputStream ) {
411+ byte [] buffer = new byte [500 ];
412+ int bytesRead = inputStream .read (buffer );
413+ if (bytesRead > 0 ) {
414+ return new String (buffer , 0 , bytesRead , StandardCharsets .UTF_8 );
415+ }
407416 }
408417 }
409418 } catch (Exception e ) {
0 commit comments