xref: /OpenGrok/opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/AnnotationController.java (revision 5082f8ec5e11b8259d833bfd5e6749eb0030ddad)
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 package org.opengrok.web.api.v1.controller;
24 
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import jakarta.servlet.http.HttpServletRequest;
27 import jakarta.servlet.http.HttpServletResponse;
28 import jakarta.ws.rs.GET;
29 import jakarta.ws.rs.Path;
30 import jakarta.ws.rs.Produces;
31 import jakarta.ws.rs.QueryParam;
32 import jakarta.ws.rs.core.Context;
33 import jakarta.ws.rs.core.MediaType;
34 import org.opengrok.indexer.history.Annotation;
35 import org.opengrok.indexer.history.HistoryGuru;
36 import org.opengrok.web.api.v1.filter.CorsEnable;
37 import org.opengrok.web.api.v1.filter.PathAuthorized;
38 import org.opengrok.web.util.NoPathParameterException;
39 
40 import java.io.File;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.List;
44 
45 import static org.opengrok.web.util.FileUtil.toFile;
46 
47 // No need to have PATH configurable.
48 @SuppressWarnings("java:S1075")
49 @Path(AnnotationController.PATH)
50 public class AnnotationController {
51 
52     public static final String PATH = "/annotation";
53 
54     static class AnnotationDTO {
55         @JsonProperty
56         private String revision;
57         @JsonProperty
58         private String author;
59         @JsonProperty
60         private String description;
61         @JsonProperty
62         private String version;
63 
64         // for testing
AnnotationDTO()65         AnnotationDTO() {
66         }
67 
AnnotationDTO(String revision, String author, String description, String version)68         AnnotationDTO(String revision, String author, String description, String version) {
69             this.revision = revision;
70             this.author = author;
71             this.description = description;
72             this.version = version;
73         }
74 
75         // for testing
getAuthor()76         public String getAuthor() {
77             return this.author;
78         }
79 
80         // for testing
getRevision()81         public String getRevision() {
82             return this.revision;
83         }
84 
85         // for testing
getDescription()86         public String getDescription() {
87             return this.description;
88         }
89 
90         // for testing
getVersion()91         public String getVersion() {
92             return this.version;
93         }
94     }
95 
96     @GET
97     @CorsEnable
98     @PathAuthorized
99     @Produces(MediaType.APPLICATION_JSON)
getContent(@ontext HttpServletRequest request, @Context HttpServletResponse response, @QueryParam("path") final String path, @QueryParam("revision") final String revision)100     public List<AnnotationDTO> getContent(@Context HttpServletRequest request,
101                                           @Context HttpServletResponse response,
102                                           @QueryParam("path") final String path,
103                                           @QueryParam("revision") final String revision)
104             throws IOException, NoPathParameterException {
105 
106         File file = toFile(path);
107 
108         Annotation annotation = HistoryGuru.getInstance().annotate(file,
109                 revision == null || revision.isEmpty() ? null : revision);
110 
111         ArrayList<AnnotationDTO> annotationList = new ArrayList<>();
112         for (int i = 1; i <= annotation.size(); i++) {
113             annotationList.add(new AnnotationDTO(annotation.getRevision(i),
114                     annotation.getAuthor(i),
115                     annotation.getDesc(annotation.getRevision(i)),
116                     annotation.getFileVersion(annotation.getRevision(i)) + "/" + annotation.getRevisions().size()));
117         }
118 
119         return annotationList;
120     }
121 }
122