|
| 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 | +} |
0 commit comments