2525import co .ipregistry .api .client .model .UserAgentList ;
2626import co .ipregistry .api .client .model .error .LookupError ;
2727import co .ipregistry .api .client .options .IpregistryOption ;
28+ import com .fasterxml .jackson .core .JsonProcessingException ;
2829import com .fasterxml .jackson .databind .DeserializationFeature ;
2930import com .fasterxml .jackson .databind .ObjectMapper ;
3031import org .apache .hc .client5 .http .config .RequestConfig ;
4041import java .io .IOException ;
4142import java .net .URLEncoder ;
4243import java .nio .charset .StandardCharsets ;
44+ import java .util .ArrayList ;
4345import java .util .Arrays ;
44- import java .util .Iterator ;
46+ import java .util .List ;
4547import java .util .concurrent .TimeUnit ;
46- import java .util .stream .Collectors ;
47- import java .util .stream .StreamSupport ;
4848
4949
5050/**
@@ -207,20 +207,9 @@ public IpInfoList lookup(final Iterable<String> ips, final IpregistryOption... o
207207
208208 @ Override
209209 public UserAgentList parse (final String ... userAgents ) throws ApiException , ClientException {
210- final StringBuilder buffer = new StringBuilder ();
211- final Iterator <String > iterator = Arrays .stream (userAgents ).iterator ();
212- while (iterator .hasNext ()) {
213- buffer .append ('"' );
214- buffer .append (iterator .next ());
215- buffer .append ('"' );
216- if (iterator .hasNext ()) {
217- buffer .append (',' );
218- }
219- }
220-
221210 try {
222211 final Object result = Request .post (config .getBaseUrl () + "/user_agent" )
223- .bodyString ("[" + buffer + "]" , ContentType .APPLICATION_JSON )
212+ .bodyString (toJsonList ( Arrays . asList ( userAgents )) , ContentType .APPLICATION_JSON )
224213 .addHeader ("authorization" , "ApiKey " + config .getApiKey ())
225214 .connectTimeout (Timeout .ofMilliseconds (config .getConnectionTimeout ()))
226215 .responseTimeout (Timeout .ofMilliseconds (config .getSocketTimeout ()))
@@ -246,11 +235,19 @@ public UserAgentList parse(final String... userAgents) throws ApiException, Clie
246235 }
247236 }
248237
249- private String toJsonList (final Iterable <String > ips ) {
250- return '[' + StreamSupport
251- .stream (ips .spliterator (), false )
252- .map (ip -> "\" " + ip + "\" " )
253- .collect (Collectors .joining ("," )) + ']' ;
238+ /**
239+ * Serializes the specified {@code values} into a JSON array, escaping each value so that
240+ * characters such as double quotes, backslashes, and control characters produce a valid
241+ * JSON body rather than a malformed or injectable one.
242+ *
243+ * @param values the values to serialize.
244+ * @return a JSON array representation of the specified {@code values}.
245+ * @throws JsonProcessingException if the values cannot be serialized.
246+ */
247+ String toJsonList (final Iterable <String > values ) throws JsonProcessingException {
248+ final List <String > list = new ArrayList <>();
249+ values .forEach (list ::add );
250+ return objectMapper .writeValueAsString (list );
254251 }
255252
256253 @ Override
0 commit comments