forked from oracle/opengrok
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add API endpoint for getting file definitions
fixes oracle#4508
- Loading branch information
Showing
4 changed files
with
201 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
*/ | ||
|
||
/* | ||
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved. | ||
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>. | ||
*/ | ||
package org.opengrok.web.api.v1.controller; | ||
|
@@ -35,16 +35,23 @@ | |
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.queryparser.classic.ParseException; | ||
import org.opengrok.indexer.analysis.AbstractAnalyzer; | ||
import org.opengrok.indexer.analysis.Definitions; | ||
import org.opengrok.indexer.index.IndexDatabase; | ||
import org.opengrok.indexer.search.QueryBuilder; | ||
import org.opengrok.web.api.v1.filter.CorsEnable; | ||
import org.opengrok.web.api.v1.filter.PathAuthorized; | ||
import org.opengrok.web.util.DTOUtil; | ||
import org.opengrok.web.util.NoPathParameterException; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.opengrok.indexer.index.IndexDatabase.getDocument; | ||
import static org.opengrok.web.util.FileUtil.toFile; | ||
|
@@ -54,6 +61,8 @@ public class FileController { | |
|
||
public static final String PATH = "file"; | ||
|
||
|
||
|
||
private StreamingOutput transfer(File file) throws FileNotFoundException { | ||
if (!file.exists()) { | ||
throw new FileNotFoundException(String.format("file %s does not exist", file)); | ||
|
@@ -141,4 +150,21 @@ public String getGenre(@Context HttpServletRequest request, | |
|
||
return genre.toString(); | ||
} | ||
|
||
@GET | ||
@CorsEnable | ||
@PathAuthorized | ||
@Path("/defs") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public List<Object> getDefinitions(@Context HttpServletRequest request, | ||
@Context HttpServletResponse response, | ||
@QueryParam("path") final String path) | ||
throws IOException, NoPathParameterException, ParseException, ClassNotFoundException { | ||
|
||
File file = toFile(path); | ||
Definitions defs = IndexDatabase.getDefinitions(file); | ||
return Optional.ofNullable(defs). | ||
map(Definitions::getTags).orElse(Collections.emptyList()). | ||
stream().map(DTOUtil::createDTO).collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,30 +18,41 @@ | |
*/ | ||
|
||
/* | ||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved. | ||
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>. | ||
*/ | ||
package org.opengrok.web.api.v1.controller; | ||
|
||
import jakarta.ws.rs.core.Application; | ||
import org.glassfish.jersey.server.ResourceConfig; | ||
import jakarta.ws.rs.core.GenericType; | ||
import org.glassfish.jersey.servlet.ServletContainer; | ||
import org.glassfish.jersey.test.DeploymentContext; | ||
import org.glassfish.jersey.test.ServletDeploymentContext; | ||
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; | ||
import org.glassfish.jersey.test.spi.TestContainerException; | ||
import org.glassfish.jersey.test.spi.TestContainerFactory; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.opengrok.indexer.analysis.Definitions; | ||
import org.opengrok.indexer.configuration.RuntimeEnvironment; | ||
import org.opengrok.indexer.history.HistoryGuru; | ||
import org.opengrok.indexer.history.RepositoryFactory; | ||
import org.opengrok.indexer.index.Indexer; | ||
import org.opengrok.indexer.util.TestRepository; | ||
import org.opengrok.web.api.v1.RestApp; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
||
class FileControllerTest extends OGKJerseyTest { | ||
|
||
|
@@ -50,10 +61,16 @@ class FileControllerTest extends OGKJerseyTest { | |
private TestRepository repository; | ||
|
||
@Override | ||
protected Application configure() { | ||
return new ResourceConfig(FileController.class); | ||
protected DeploymentContext configureDeployment() { | ||
return ServletDeploymentContext.forServlet(new ServletContainer(new RestApp())).build(); | ||
} | ||
|
||
@Override | ||
protected TestContainerFactory getTestContainerFactory() throws TestContainerException { | ||
return new GrizzlyWebTestContainerFactory(); | ||
} | ||
|
||
|
||
@BeforeEach | ||
@Override | ||
public void setUp() throws Exception { | ||
|
@@ -113,4 +130,25 @@ void testFileGenre() { | |
.get(String.class); | ||
assertEquals("PLAIN", genre); | ||
} | ||
|
||
@Test | ||
void testFileDefinitions() { | ||
final String path = "git/main.c"; | ||
GenericType<List<Definitions.Tag>> type = new GenericType<>() { | ||
}; | ||
List<Definitions.Tag> defs = target("file") | ||
.path("defs") | ||
.queryParam("path", path) | ||
.request() | ||
.get(type); | ||
assertFalse(defs.isEmpty()); | ||
assertAll(() -> assertFalse(defs.stream().map(Definitions.Tag::getType).anyMatch(Objects::isNull)), | ||
() -> assertFalse(defs.stream().map(Definitions.Tag::getSymbol).anyMatch(Objects::isNull)), | ||
() -> assertFalse(defs.stream().map(Definitions.Tag::getText).anyMatch(Objects::isNull)), | ||
() -> assertFalse(defs.stream().filter(e -> !e.getType().equals("local")). | ||
map(Definitions.Tag::getSignature).anyMatch(Objects::isNull)), | ||
() -> assertFalse(defs.stream().map(Definitions.Tag::getLine).anyMatch(e -> e <= 0)), | ||
() -> assertFalse(defs.stream().map(Definitions.Tag::getLineStart).anyMatch(e -> e <= 0)), | ||
() -> assertFalse(defs.stream().map(Definitions.Tag::getLineEnd).anyMatch(e -> e <= 0))); | ||
} | ||
} |