View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.search.backend.remoterepository.extractor;
20  
21  import java.util.List;
22  
23  import org.apache.maven.search.api.Record;
24  import org.apache.maven.search.backend.remoterepository.Context;
25  import org.apache.maven.search.backend.remoterepository.RecordFactory;
26  import org.jsoup.nodes.Document;
27  import org.jsoup.nodes.Element;
28  import org.jsoup.select.Elements;
29  
30  /**
31   * Extractor for Sonatype Nexus2.
32   */
33  public class Nx2ResponseExtractor extends ResponseExtractorSupport {
34      protected boolean accept(String name) {
35          return !"Parent Directory".equals(name) && super.accept(name);
36      }
37  
38      private String name(Element element) {
39          String name = element.text();
40          if (name.endsWith("/")) {
41              name = name.substring(0, name.length() - 1);
42          }
43          return name;
44      }
45  
46      @Override
47      public int populateG(Context context, Document document, RecordFactory recordFactory, List<Record> page) {
48          // Index HTML page like this one:
49          // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/
50          Elements elements = document.getElementsByTag("a");
51          for (Element element : elements) {
52              String name = name(element);
53              if (accept(name)) {
54                  page.add(recordFactory.create(context.getGroupId(), name, null, null, null, null));
55              }
56          }
57          return page.size();
58      }
59  
60      @Override
61      public int populateGAV(Context context, Document document, RecordFactory recordFactory, List<Record> page) {
62          // Index HTML page like this one:
63          // https://repo.maven.apache.org/maven2/org/apache/maven/indexer/search-api/7.0.3/
64          Elements elements = document.getElementsByTag("a");
65          for (Element element : elements) {
66              // skip possible subdirectories and files without extensions
67              String name = element.attr("href");
68              if (name.endsWith("/") || !name.contains(".")) {
69                  continue;
70              }
71              populateGAVName(context, name(element), recordFactory, page);
72          }
73          return page.size();
74      }
75  }