-
Notifications
You must be signed in to change notification settings - Fork 781
file content/genre API #3050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
file content/genre API #3050
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e0938fe
refactor path authnz filter to utility class
6afb0a9
first stab at file content API endpoint
2155b84
TODO: filter
74a3f98
add line numbers
ecd22ec
return content for text files only
6990c0e
- refactor path authorization to PathAuthorizationFilter
c8d0acc
enforce path presence
feaa479
add tests, convert to traditional loop
fdbc725
remove not needed imports
1912189
use PATH_PARAM in log message
09214fa
use T field to ensure plaintext
a2243a3
fix javadoc
738c38e
fix test
9d5b4cd
remove unused import
cda7140
use partial indexing
a72b9bf
add detail
1382ace
fix style
fa2ee86
address review comments
ef3858f
add more types
f41f611
remove unused import
3ec34b6
remove JSON
f5a474b
catch FileNotFoundException
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
177 changes: 177 additions & 0 deletions
177
opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/FileController.java
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/* | ||
* CDDL HEADER START | ||
* | ||
* The contents of this file are subject to the terms of the | ||
* Common Development and Distribution License (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* | ||
* See LICENSE.txt included in this distribution for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
* When distributing Covered Code, include this CDDL HEADER in each | ||
* file and include the License file at LICENSE.txt. | ||
* If applicable, add the following below this CDDL HEADER, with the | ||
* fields enclosed by brackets "[]" replaced with your own identifying | ||
* information: Portions Copyright [yyyy] [name of copyright owner] | ||
* | ||
* CDDL HEADER END | ||
*/ | ||
|
||
/* | ||
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. | ||
*/ | ||
|
||
package org.opengrok.web.api.v1.controller; | ||
|
||
import org.apache.lucene.document.Document; | ||
import org.apache.lucene.queryparser.classic.ParseException; | ||
import org.opengrok.indexer.analysis.AbstractAnalyzer; | ||
import org.opengrok.indexer.configuration.RuntimeEnvironment; | ||
import org.opengrok.indexer.search.QueryBuilder; | ||
import org.opengrok.web.api.v1.filter.CorsEnable; | ||
import org.opengrok.web.api.v1.filter.PathAuthorized; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.StreamingOutput; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import static org.opengrok.indexer.index.IndexDatabase.getDocument; | ||
|
||
@Path(FileController.PATH) | ||
public class FileController { | ||
|
||
public static final String PATH = "/file"; | ||
|
||
private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance(); | ||
|
||
private static File getFile(String path, HttpServletResponse response) throws IOException { | ||
if (path == null) { | ||
vladak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (response != null) { | ||
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing path parameter"); | ||
} | ||
return null; | ||
} | ||
|
||
File file = new File(env.getSourceRootFile(), path); | ||
if (!file.isFile()) { | ||
if (response != null) { | ||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "File not found"); | ||
} | ||
return null; | ||
} | ||
|
||
return file; | ||
} | ||
|
||
private StreamingOutput transfer(File file) throws FileNotFoundException { | ||
InputStream in = new FileInputStream(file); | ||
return out -> { | ||
byte[] buffer = new byte[1024]; | ||
int len = in.read(buffer); | ||
while (len != -1) { | ||
out.write(buffer, 0, len); | ||
len = in.read(buffer); | ||
} | ||
}; | ||
} | ||
|
||
@GET | ||
@CorsEnable | ||
@PathAuthorized | ||
@Path("/content") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public StreamingOutput getContentPlain(@Context HttpServletRequest request, | ||
@Context HttpServletResponse response, | ||
@QueryParam("path") final String path) throws IOException, ParseException { | ||
|
||
File file = getFile(path, response); | ||
if (file == null) { | ||
// error already set in the response | ||
return null; | ||
} | ||
|
||
Document doc; | ||
if ((doc = getDocument(file)) == null) { | ||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot get document for file"); | ||
return null; | ||
} | ||
|
||
String fileType = doc.get(QueryBuilder.T); | ||
if (!AbstractAnalyzer.Genre.PLAIN.typeName().equals(fileType)) { | ||
response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE, "Not a text file"); | ||
vladak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
} | ||
|
||
return transfer(file); | ||
} | ||
|
||
@GET | ||
@CorsEnable | ||
@PathAuthorized | ||
@Path("/content") | ||
@Produces(MediaType.APPLICATION_OCTET_STREAM) | ||
public StreamingOutput getContentOctets(@Context HttpServletRequest request, | ||
@Context HttpServletResponse response, | ||
@QueryParam("path") final String path) throws IOException, ParseException { | ||
|
||
File file = getFile(path, response); | ||
if (file == null) { | ||
// error already set in the response | ||
return null; | ||
} | ||
|
||
Document doc; | ||
if ((doc = getDocument(file)) == null) { | ||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot get document for file"); | ||
return null; | ||
} | ||
|
||
try { | ||
return transfer(file); | ||
} catch (FileNotFoundException e) { | ||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot find file"); | ||
return null; | ||
} | ||
} | ||
|
||
@GET | ||
@CorsEnable | ||
@PathAuthorized | ||
@Path("/genre") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String getGenre(@Context HttpServletRequest request, | ||
@Context HttpServletResponse response, | ||
@QueryParam("path") final String path) throws IOException, ParseException { | ||
|
||
File file = getFile(path, response); | ||
if (file == null) { | ||
// error already set in the response | ||
return null; | ||
vladak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
Document doc; | ||
if ((doc = getDocument(file)) == null) { | ||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot get document for file"); | ||
return null; | ||
} | ||
|
||
AbstractAnalyzer.Genre genre = AbstractAnalyzer.Genre.get(doc.get(QueryBuilder.T)); | ||
if (genre == null) { | ||
response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot get genre from the document"); | ||
return null; | ||
} | ||
|
||
return genre.toString(); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.