Skip to content

Commit 6afb0a9

Browse files
author
Vladimir Kotal
committed
first stab at file content API endpoint
- no line number support fixes oracle#3048
1 parent e0938fe commit 6afb0a9

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

apiary.apib

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ Besides `/suggester` and `/search` endpoints, everything is accessible from `loc
6767

6868
+ Response 204
6969

70+
## File content [/filecontent{?path}]
71+
72+
### get history entries [GET]
73+
74+
+ Parameters
75+
+ path (string) - path of file/directory to get history for, relative to source root
76+
77+
+ Response 200 (application/json)
78+
+ Body
79+
80+
XXX
81+
7082
## History [/history{?path,withFiles,start,max}]
7183

7284
### get history entries [GET]
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
24+
package org.opengrok.web.api.v1.controller;
25+
26+
import com.fasterxml.jackson.annotation.JsonProperty;
27+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
28+
import org.opengrok.web.api.v1.filter.CorsEnable;
29+
30+
import javax.servlet.http.HttpServletRequest;
31+
import javax.servlet.http.HttpServletResponse;
32+
import javax.ws.rs.GET;
33+
import javax.ws.rs.Path;
34+
import javax.ws.rs.Produces;
35+
import javax.ws.rs.QueryParam;
36+
import javax.ws.rs.core.Context;
37+
import javax.ws.rs.core.MediaType;
38+
import javax.ws.rs.core.Response;
39+
import java.io.BufferedReader;
40+
import java.io.File;
41+
import java.io.FileReader;
42+
import java.io.IOException;
43+
import java.util.ArrayList;
44+
45+
import static org.opengrok.web.util.AuthPathUtil.isPathAuthorized;
46+
47+
@Path(FileContentController.PATH)
48+
public class FileContentController {
49+
50+
public static final String PATH = "/filecontent";
51+
52+
private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
53+
54+
class FileContentDTO {
55+
@JsonProperty
56+
ArrayList<String> lines;
57+
58+
FileContentDTO() {
59+
lines = new ArrayList<>();
60+
}
61+
62+
public ArrayList<String> getLines() {
63+
return lines;
64+
}
65+
66+
public void add(String line) {
67+
lines.add(line);
68+
}
69+
}
70+
71+
@GET
72+
@CorsEnable
73+
@Produces(MediaType.APPLICATION_JSON)
74+
public Object get(@Context HttpServletRequest request,
75+
@Context HttpServletResponse response,
76+
@QueryParam("path") final String path) throws IOException {
77+
78+
if (!isPathAuthorized(path, request)) {
79+
response.sendError(Response.status(Response.Status.FORBIDDEN).build().getStatus(),
80+
"not authorized");
81+
return null;
82+
}
83+
84+
// TODO: identify the file type and return only for text files
85+
86+
File file = new File(env.getSourceRootFile(), path);
87+
if (!file.isFile()) {
88+
// TODO: set error
89+
return null;
90+
}
91+
FileContentDTO fileContent = new FileContentDTO();
92+
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
93+
String line;
94+
while ((line = br.readLine()) != null) {
95+
fileContent.add(line);
96+
}
97+
}
98+
99+
// TODO: array of lines with line number
100+
return fileContent.getLines();
101+
}
102+
}

opengrok-web/src/main/java/org/opengrok/web/api/v1/filter/LocalhostFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
package org.opengrok.web.api.v1.filter;
2424

2525
import org.opengrok.indexer.logger.LoggerFactory;
26+
import org.opengrok.web.api.v1.controller.FileContentController;
2627
import org.opengrok.web.api.v1.controller.HistoryController;
2728
import org.opengrok.web.api.v1.controller.SearchController;
2829
import org.opengrok.web.api.v1.controller.SuggesterController;
@@ -58,7 +59,7 @@ public class LocalhostFilter implements ContainerRequestFilter {
5859
*/
5960
private static final Set<String> allowedPaths = new HashSet<>(Arrays.asList(
6061
SearchController.PATH, SuggesterController.PATH, SuggesterController.PATH + "/config",
61-
HistoryController.PATH));
62+
HistoryController.PATH, FileContentController.PATH));
6263

6364
@Context
6465
private HttpServletRequest request;

0 commit comments

Comments
 (0)