Skip to content

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 22 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions apiary.apib
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,35 @@ Besides `/suggester` and `/search` endpoints, everything is accessible from `loc

+ Response 204

## File content [/file/content{?path}]

### get file content [GET]

Honors the Accept header. The text type works for plain text files only.

+ Parameters
+ path (string) - path of file, relative to source root

+ Response 200 (text/plain)
+ Body

foo
bar

+ Response 200 (application/octet-stream)

## File genre [/file/genre{?path}]

### get file genre [GET]

+ Parameters
+ path (string) - path of file, relative to source root

+ Response 200 (text/plain)
+ Body

genre as identified by analyzer, could be PLAIN, XREFABLE, IMAGE, DATA, HTML

## History [/history{?path,withFiles,start,max}]

### get history entries [GET]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,9 @@ private boolean isInterrupted() {
* @param listener the object to receive the events
*/
public void addIndexChangedListener(IndexChangedListener listener) {
listeners.add(listener);
if (listener != null) {
listeners.add(listener);
}
}

/**
Expand Down Expand Up @@ -1568,8 +1570,29 @@ public static IndexReader getIndexReader(String path) {
* @throws ClassNotFoundException if the class for the stored definitions
* instance cannot be found
*/
public static Definitions getDefinitions(File file)
throws IOException, ParseException, ClassNotFoundException {
public static Definitions getDefinitions(File file) throws ParseException, IOException, ClassNotFoundException {
Document doc = getDocument(file);
if (doc == null) {
return null;
}

IndexableField tags = doc.getField(QueryBuilder.TAGS);
if (tags != null) {
return Definitions.deserialize(tags.binaryValue().bytes);
}

// Didn't find any definitions.
return null;
}

/**
* @param file File object of a file under source root
* @return Document object for the file or {@code null}
* @throws IOException
* @throws ParseException
*/
public static Document getDocument(File file)
throws IOException, ParseException {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
String path;
try {
Expand All @@ -1584,34 +1607,31 @@ public static Definitions getDefinitions(File file)
IndexReader ireader = getIndexReader(path);

if (ireader == null) {
// No index, no definitions...
// No index, no document..
return null;
}

try {
Document doc;
Query q = new QueryBuilder().setPath(path).build();
IndexSearcher searcher = new IndexSearcher(ireader);
TopDocs top = searcher.search(q, 1);
if (top.totalHits.value == 0) {
// No hits, no definitions...
// No hits, no document...
return null;
}
Document doc = searcher.doc(top.scoreDocs[0].doc);
doc = searcher.doc(top.scoreDocs[0].doc);
String foundPath = doc.get(QueryBuilder.PATH);

// Only use the definitions if we found an exact match.
if (path.equals(foundPath)) {
IndexableField tags = doc.getField(QueryBuilder.TAGS);
if (tags != null) {
return Definitions.deserialize(tags.binaryValue().bytes);
}
// Only use the document if we found an exact match.
if (!path.equals(foundPath)) {
return null;
}

return doc;
} finally {
ireader.close();
}

// Didn't find any definitions.
return null;
}

@Override
Expand Down
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) {
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");
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;
}

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@
package org.opengrok.web.api.v1.controller;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.opengrok.indexer.authorization.AuthorizationFramework;
import org.opengrok.indexer.configuration.Project;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
import org.opengrok.indexer.history.History;
import org.opengrok.indexer.history.HistoryEntry;
import org.opengrok.indexer.history.HistoryException;
import org.opengrok.indexer.history.HistoryGuru;
import org.opengrok.indexer.web.messages.JSONable;
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;
Expand All @@ -43,9 +42,7 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -193,26 +190,15 @@ static HistoryDTO getHistoryDTO(List<HistoryEntry> historyEntries, int start, in

@GET
@CorsEnable
@PathAuthorized
@Produces(MediaType.APPLICATION_JSON)
public HistoryDTO get(@Context HttpServletRequest request,
@Context HttpServletResponse response,
@QueryParam("path") final String path,
@QueryParam("withFiles") final boolean withFiles,
@QueryParam("max") @DefaultValue(MAX_RESULTS + "") final int maxEntries,
@QueryParam("start") @DefaultValue(0 + "") final int startIndex)
throws HistoryException, IOException {

if (request != null) {
AuthorizationFramework auth = env.getAuthorizationFramework();
if (auth != null) {
Project p = Project.getProject(path.startsWith("/") ? path : "/" + path);
if (p != null && !auth.isAllowed(request, p)) {
response.sendError(Response.status(Response.Status.FORBIDDEN).build().getStatus(),
"not authorized");
return null;
}
}
}
throws HistoryException {

History history = HistoryGuru.getInstance().getHistory(new File(env.getSourceRootFile(), path),
withFiles, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package org.opengrok.web.api.v1.filter;

import org.opengrok.indexer.logger.LoggerFactory;
import org.opengrok.web.api.v1.controller.FileController;
import org.opengrok.web.api.v1.controller.HistoryController;
import org.opengrok.web.api.v1.controller.SearchController;
import org.opengrok.web.api.v1.controller.SuggesterController;
Expand Down Expand Up @@ -58,7 +59,7 @@ public class LocalhostFilter implements ContainerRequestFilter {
*/
private static final Set<String> allowedPaths = new HashSet<>(Arrays.asList(
SearchController.PATH, SuggesterController.PATH, SuggesterController.PATH + "/config",
HistoryController.PATH));
HistoryController.PATH, FileController.PATH));

@Context
private HttpServletRequest request;
Expand Down
Loading