2727import java .io .InputStreamReader ;
2828import java .io .OutputStreamWriter ;
2929import java .io .PrintStream ;
30- import java .io .UnsupportedEncodingException ;
3130import java .net .HttpURLConnection ;
3231import java .net .InetSocketAddress ;
3332import java .net .MalformedURLException ;
9190import org .zaproxy .clientapi .gen .Users ;
9291import org .zaproxy .clientapi .gen .Wappalyzer ;
9392import org .zaproxy .clientapi .gen .Websocket ;
93+ import org .zaproxy .clientapi .impl .DefaultAlertService ;
94+ import org .zaproxy .clientapi .service .AlertService ;
9495
9596@ SuppressWarnings ("this-escape" )
9697public class ClientApi {
@@ -103,10 +104,10 @@ public class ClientApi {
103104 private boolean debug = false ;
104105 private PrintStream debugStream = System .out ;
105106
106- private final String zapAddress ;
107- private final int zapPort ;
107+ private String zapAddress ;
108+ private int zapPort ;
108109
109- private final String apiKey ;
110+ private String apiKey ;
110111
111112 private DocumentBuilderFactory docBuilderFactory ;
112113
@@ -171,9 +172,11 @@ public class ClientApi {
171172 public Users users = new Users (this );
172173 public Wappalyzer wappalyzer = new Wappalyzer (this );
173174 public Websocket websocket = new Websocket (this );
175+ private AlertService alertService ;
174176
175177 public ClientApi (String zapAddress , int zapPort ) {
176178 this (zapAddress , zapPort , false );
179+ this .alertService = new DefaultAlertService (this .alert );
177180 }
178181
179182 /**
@@ -187,10 +190,12 @@ public ClientApi(String zapAddress, int zapPort) {
187190 */
188191 public ClientApi (String zapAddress , int zapPort , String apiKey ) {
189192 this (zapAddress , zapPort , apiKey , false );
193+ this .alertService = new DefaultAlertService (this .alert );
190194 }
191195
192196 public ClientApi (String zapAddress , int zapPort , boolean debug ) {
193197 this (zapAddress , zapPort , null , debug );
198+ this .alertService = new DefaultAlertService (this .alert );
194199 }
195200
196201 /**
@@ -212,6 +217,7 @@ public ClientApi(String zapAddress, int zapPort, String apiKey, boolean debug) {
212217 this .zapAddress = zapAddress ;
213218 this .zapPort = zapPort ;
214219 this .apiKey = apiKey ;
220+ this .alertService = new DefaultAlertService (this .alert );
215221 }
216222
217223 public void setDebugStream (PrintStream debugStream ) {
@@ -228,43 +234,15 @@ private int statusToInt(ApiResponse response) {
228234
229235 public void checkAlerts (List <Alert > ignoreAlerts , List <Alert > requireAlerts )
230236 throws ClientApiException {
231- Map <String , List <Alert >> results = checkForAlerts (ignoreAlerts , requireAlerts );
232- verifyAlerts (results .get ("requireAlerts" ), results .get ("reportAlerts" ));
233- }
234-
235- private void verifyAlerts (List <Alert > requireAlerts , List <Alert > reportAlerts )
236- throws ClientApiException {
237- StringBuilder sb = new StringBuilder ();
238- if (reportAlerts .size () > 0 ) {
239- sb .append ("Found " ).append (reportAlerts .size ()).append (" alerts\n " );
240- for (Alert alert : reportAlerts ) {
241- sb .append ('\t' );
242- sb .append (alert .toString ());
243- sb .append ('\n' );
244- }
245- }
246- if (requireAlerts != null && requireAlerts .size () > 0 ) {
247- if (sb .length () > 0 ) {
248- sb .append ('\n' );
249- }
250- sb .append ("Not found " ).append (requireAlerts .size ()).append (" alerts\n " );
251- for (Alert alert : requireAlerts ) {
252- sb .append ('\t' );
253- sb .append (alert .toString ());
254- sb .append ('\n' );
255- }
256- }
257- if (sb .length () > 0 ) {
258- if (debug ) {
259- debugStream .println ("Failed: " + sb .toString ());
260- }
261- throw new ClientApiException (sb .toString ());
262- }
237+ Map <String , List <Alert >> results =
238+ alertService .checkAlerts (ignoreAlerts , requireAlerts , alert );
239+ alertService .verifyAlerts (results .get ("requireAlerts" ), results .get ("reportAlerts" ));
263240 }
264241
265242 public void checkAlerts (List <Alert > ignoreAlerts , List <Alert > requireAlerts , File outputFile )
266243 throws ClientApiException {
267- Map <String , List <Alert >> results = checkForAlerts (ignoreAlerts , requireAlerts );
244+ Map <String , List <Alert >> results =
245+ alertService .checkAlerts (ignoreAlerts , requireAlerts , alert );
268246 int alertsFound = results .get ("reportAlerts" ).size ();
269247 int alertsNotFound = results .get ("requireAlerts" ).size ();
270248 int alertsIgnored = results .get ("ignoredAlerts" ).size ();
@@ -290,62 +268,6 @@ public void checkAlerts(List<Alert> ignoreAlerts, List<Alert> requireAlerts, Fil
290268 }
291269 }
292270
293- public List <Alert > getAlerts (String baseUrl , int start , int count ) throws ClientApiException {
294- List <Alert > alerts = new ArrayList <>();
295- ApiResponse response =
296- alert .alerts (baseUrl , String .valueOf (start ), String .valueOf (count ), null );
297- if (response != null && response instanceof ApiResponseList ) {
298- ApiResponseList alertList = (ApiResponseList ) response ;
299- for (ApiResponse resp : alertList .getItems ()) {
300- alerts .add (new Alert ((ApiResponseSet ) resp ));
301- }
302- }
303- return alerts ;
304- }
305-
306- private Map <String , List <Alert >> checkForAlerts (
307- List <Alert > ignoreAlerts , List <Alert > requireAlerts ) throws ClientApiException {
308- List <Alert > reportAlerts = new ArrayList <>();
309- List <Alert > ignoredAlerts = new ArrayList <>();
310- List <Alert > alerts = getAlerts (null , -1 , -1 );
311- for (Alert alert : alerts ) {
312- boolean ignore = false ;
313- if (ignoreAlerts != null ) {
314- for (Alert ignoreAlert : ignoreAlerts ) {
315- if (alert .matches (ignoreAlert )) {
316- if (debug ) {
317- debugStream .println ("Ignoring alert " + ignoreAlert );
318- }
319- ignoredAlerts .add (alert );
320- ignore = true ;
321- break ;
322- }
323- }
324- }
325- if (!ignore ) {
326- reportAlerts .add (alert );
327- }
328- if (requireAlerts != null ) {
329- for (Alert requireAlert : requireAlerts ) {
330- if (alert .matches (requireAlert )) {
331- if (debug ) {
332- debugStream .println ("Found alert " + alert );
333- }
334- requireAlerts .remove (requireAlert );
335- // Remove it from the not-ignored list as well
336- reportAlerts .remove (alert );
337- break ;
338- }
339- }
340- }
341- }
342- HashMap <String , List <Alert >> results = new HashMap <>();
343- results .put ("reportAlerts" , reportAlerts );
344- results .put ("requireAlerts" , requireAlerts );
345- results .put ("ignoredAlerts" , ignoredAlerts );
346- return results ;
347- }
348-
349271 private void accessUrlViaProxy (Proxy proxy , String apiurl ) throws ClientApiException {
350272 try {
351273 URL url = createUrl (apiurl );
@@ -502,17 +424,14 @@ private byte[] getBytes(
502424 if (debug ) {
503425 debugStream .println ("Open URL: " + request .getRequestUri ());
504426 }
505- InputStream in = getConnectionInputStream (request );
506427 ByteArrayOutputStream out = new ByteArrayOutputStream ();
507428 byte [] buffer = new byte [8 * 1024 ];
508- try {
429+ try (InputStream in = getConnectionInputStream (request );
430+ out ) {
509431 int bytesRead ;
510432 while ((bytesRead = in .read (buffer )) != -1 ) {
511433 out .write (buffer , 0 , bytesRead );
512434 }
513- } finally {
514- out .close ();
515- in .close ();
516435 }
517436 return out .toByteArray ();
518437
@@ -589,12 +508,7 @@ private static StringBuilder appendParams(Map<String, String> params, StringBuil
589508 }
590509
591510 private static String encodeQueryParam (String param ) {
592- try {
593- return URLEncoder .encode (param , "UTF-8" );
594- } catch (UnsupportedEncodingException ignore ) {
595- // UTF-8 is a standard charset.
596- }
597- return param ;
511+ return URLEncoder .encode (param , StandardCharsets .UTF_8 );
598512 }
599513
600514 /**
@@ -692,7 +606,7 @@ public void includeOneMatchingNodeInContext(String contextName, String regex) th
692606 private List <String > getSessionUrls () throws Exception {
693607 List <String > sessionUrls = new ArrayList <>();
694608 ApiResponse response = core .urls ();
695- if (response != null && response instanceof ApiResponseList ) {
609+ if (response instanceof ApiResponseList ) {
696610 ApiResponseElement urlList =
697611 (ApiResponseElement ) ((ApiResponseList ) response ).getItems ().get (0 );
698612 for (ApiResponse element : ((ApiResponseList ) response ).getItems ()) {
@@ -742,7 +656,7 @@ private void waitForAScanToFinish(String targetUrl) throws ClientApiException {
742656 status = statusToInt (ascan .status ("" ));
743657 if (debug ) {
744658 String format = "Scanning %s Progress: %d%%" ;
745- System .out .println ( String . format ( format , targetUrl , status ) );
659+ System .out .printf ( format + "%n" , targetUrl , status );
746660 }
747661 try {
748662 Thread .sleep (1000 );
0 commit comments