View Javadoc
1   /*
2    * This file is part of dependency-check-utils.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Copyright (c) 2024 Hans Aikema. All Rights Reserved.
17   */
18  package org.owasp.dependencycheck.utils;
19  
20  import org.apache.hc.client5.http.impl.classic.AbstractHttpClientResponseHandler;
21  import org.apache.hc.core5.http.HttpEntity;
22  import org.w3c.dom.Document;
23  import org.xml.sax.SAXException;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  import javax.xml.parsers.ParserConfigurationException;
27  import java.io.IOException;
28  import java.io.InputStream;
29  
30  public class ToXMLDocumentResponseHandler extends AbstractHttpClientResponseHandler<Document> {
31      @Override
32      public Document handleEntity(HttpEntity entity) throws IOException {
33          try (InputStream in = entity.getContent()) {
34              final DocumentBuilder builder = XmlUtils.buildSecureDocumentBuilder();
35              return builder.parse(in);
36          } catch (ParserConfigurationException | SAXException | IOException e) {
37              final String errorMessage = "Failed to parse XML Response: " + e.getMessage();
38              throw new IOException(errorMessage, e);
39          }
40      }
41  }