public RemoteRepositorySearchResponse search()

in search-backend-remoterepository/src/main/java/org/apache/maven/search/backend/remoterepository/internal/RemoteRepositorySearchBackendImpl.java [112:203]


    public RemoteRepositorySearchResponse search(SearchRequest searchRequest) throws IOException {
        Context context = new Context(searchRequest);
        String uri = baseUri;
        State state = null;
        if (context.getGroupId() != null) {
            uri += context.getGroupId().replace('.', '/') + "/";
            state = State.G;
            if (context.getArtifactId() != null) {
                uri += context.getArtifactId() + "/";
                state = State.GA;
                if (context.getVersion() == null) {
                    uri += "maven-metadata.xml";
                } else {
                    uri += context.getVersion() + "/";
                    state = State.GAV;
                    if (context.getFileExtension() != null) {
                        // we go for actually specified artifact
                        uri += context.getArtifactId() + "-" + context.getVersion();
                        if (context.getClassifier() != null) {
                            uri += "-" + context.getClassifier();
                        }
                        uri += "." + context.getFileExtension();
                        state = State.GAVCE;
                        if (context.getSha1() != null) {
                            state = State.GAVCE1;
                        }
                    }
                }
            }
        }
        if (state == null) {
            throw new IllegalArgumentException("Unsupported Query: " + searchRequest.getQuery());
        }

        int totalHits = 0;
        List<Record> page = new ArrayList<>(searchRequest.getPaging().getPageSize());
        RecordFactory recordFactory = new RecordFactory(this);
        Document document = null;
        if (state.ordinal() < State.GAVCE.ordinal()) {
            Parser parser = state == State.GA ? Parser.xmlParser() : Parser.htmlParser();
            try (RemoteRepositorySearchTransport.Response response = transport.get(uri, commonHeaders)) {
                if (response.getCode() == 200) {
                    document = Jsoup.parse(response.getBody(), StandardCharsets.UTF_8.name(), uri, parser);
                }
            }

            if (document == null) {
                throw new IOException("Unexpected response from: " + uri);
            }

            switch (state) {
                case G:
                    totalHits = responseExtractor.populateG(context, document, recordFactory, page);
                    break;
                case GA:
                    totalHits = responseExtractor.populateGA(context, document, recordFactory, page);
                    break;
                case GAV:
                    totalHits = responseExtractor.populateGAV(context, document, recordFactory, page);
                    break;
                default:
                    throw new IllegalStateException("State" + state); // checkstyle
            }
        } else {
            try (RemoteRepositorySearchTransport.Response response = transport.head(uri, commonHeaders)) {
                if (response.getCode() == 200) {
                    boolean matches = context.getSha1() == null;
                    if (context.getSha1() != null) {
                        try (RemoteRepositorySearchTransport.Response sha1Response =
                                transport.get(uri + ".sha1", commonHeaders)) {
                            if (response.getCode() == 200) {
                                try (InputStream body = sha1Response.getBody()) {
                                    String remoteSha1 = readChecksum(body);
                                    matches = Objects.equals(context.getSha1(), remoteSha1);
                                }
                            }
                        }
                    }
                    if (matches) {
                        page.add(recordFactory.create(
                                context.getGroupId(),
                                context.getArtifactId(),
                                context.getVersion(),
                                context.getClassifier(),
                                context.getFileExtension()));
                        totalHits = 1;
                    }
                }
            }
        }
        return new RemoteRepositorySearchResponseImpl(searchRequest, totalHits, page, uri, document);
    }