|
| 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.authorization.AuthorizationFramework; |
| 28 | +import org.opengrok.indexer.configuration.Project; |
| 29 | +import org.opengrok.indexer.configuration.RuntimeEnvironment; |
| 30 | +import org.opengrok.indexer.history.History; |
| 31 | +import org.opengrok.indexer.history.HistoryEntry; |
| 32 | +import org.opengrok.indexer.history.HistoryException; |
| 33 | +import org.opengrok.indexer.history.HistoryGuru; |
| 34 | +import org.opengrok.indexer.web.messages.JSONable; |
| 35 | +import org.opengrok.web.api.v1.filter.CorsEnable; |
| 36 | + |
| 37 | +import javax.servlet.http.HttpServletRequest; |
| 38 | +import javax.servlet.http.HttpServletResponse; |
| 39 | +import javax.ws.rs.DefaultValue; |
| 40 | +import javax.ws.rs.GET; |
| 41 | +import javax.ws.rs.Path; |
| 42 | +import javax.ws.rs.Produces; |
| 43 | +import javax.ws.rs.QueryParam; |
| 44 | +import javax.ws.rs.core.Context; |
| 45 | +import javax.ws.rs.core.MediaType; |
| 46 | +import javax.ws.rs.core.Response; |
| 47 | +import java.io.File; |
| 48 | +import java.io.IOException; |
| 49 | +import java.util.ArrayList; |
| 50 | +import java.util.Date; |
| 51 | +import java.util.List; |
| 52 | +import java.util.Objects; |
| 53 | +import java.util.SortedSet; |
| 54 | + |
| 55 | +@Path(HistoryController.PATH) |
| 56 | +public final class HistoryController { |
| 57 | + |
| 58 | + private static final RuntimeEnvironment env = RuntimeEnvironment.getInstance(); |
| 59 | + |
| 60 | + private static final int MAX_RESULTS = 1000; |
| 61 | + |
| 62 | + public static final String PATH = "/history"; |
| 63 | + |
| 64 | + static class HistoryEntryDTO implements JSONable { |
| 65 | + @JsonProperty |
| 66 | + private String revision; |
| 67 | + @JsonProperty |
| 68 | + private Date date; |
| 69 | + @JsonProperty |
| 70 | + private String author; |
| 71 | + @JsonProperty |
| 72 | + private String tags; |
| 73 | + @JsonProperty |
| 74 | + private String message; |
| 75 | + @JsonProperty |
| 76 | + private SortedSet<String> files; |
| 77 | + |
| 78 | + // for testing |
| 79 | + HistoryEntryDTO() { |
| 80 | + } |
| 81 | + |
| 82 | + HistoryEntryDTO(HistoryEntry entry) { |
| 83 | + this.revision = entry.getRevision(); |
| 84 | + this.date = entry.getDate(); |
| 85 | + this.author = entry.getAuthor(); |
| 86 | + this.tags = entry.getTags(); |
| 87 | + this.message = entry.getMessage(); |
| 88 | + this.files = entry.getFiles(); |
| 89 | + } |
| 90 | + |
| 91 | + // for testing |
| 92 | + public String getAuthor() { |
| 93 | + return author; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean equals(Object obj) { |
| 98 | + if (obj == null) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + if (getClass() != obj.getClass()) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + final HistoryEntryDTO other = (HistoryEntryDTO) obj; |
| 105 | + if (!Objects.equals(this.revision, other.revision)) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + if (!Objects.equals(this.date, other.date)) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + if (!Objects.equals(this.author, other.author)) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + if (!Objects.equals(this.tags, other.tags)) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + if (!Objects.equals(this.message, other.message)) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + if (!Objects.equals(this.files, other.files)) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int hashCode() { |
| 128 | + return Objects.hash(revision, date, author, tags, message, files); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + static class HistoryDTO implements JSONable { |
| 133 | + @JsonProperty |
| 134 | + private final List<HistoryEntryDTO> entries; |
| 135 | + @JsonProperty |
| 136 | + private int start; |
| 137 | + @JsonProperty |
| 138 | + private int count; |
| 139 | + @JsonProperty |
| 140 | + private int total; |
| 141 | + |
| 142 | + // for testing |
| 143 | + HistoryDTO() { |
| 144 | + this.entries = new ArrayList<>(); |
| 145 | + } |
| 146 | + |
| 147 | + HistoryDTO(List<HistoryEntryDTO> entries, int start, int count, int total) { |
| 148 | + this.entries = entries; |
| 149 | + this.start = start; |
| 150 | + this.count = count; |
| 151 | + this.total = total; |
| 152 | + } |
| 153 | + |
| 154 | + // for testing |
| 155 | + public List<HistoryEntryDTO> getEntries() { |
| 156 | + return entries; |
| 157 | + } |
| 158 | + |
| 159 | + public boolean equals(Object obj) { |
| 160 | + if (obj == null) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + if (getClass() != obj.getClass()) { |
| 164 | + return false; |
| 165 | + } |
| 166 | + final HistoryDTO other = (HistoryDTO) obj; |
| 167 | + if (!Objects.equals(this.entries, other.entries)) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + if (!Objects.equals(this.start, other.start)) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + if (!Objects.equals(this.count, other.count)) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + if (!Objects.equals(this.total, other.total)) { |
| 177 | + return false; |
| 178 | + } |
| 179 | + return true; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public int hashCode() { |
| 184 | + return Objects.hash(entries, start, count, total); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + static HistoryDTO getHistoryDTO(List<HistoryEntry> historyEntries, int start, int count, int total) { |
| 189 | + List<HistoryEntryDTO> entries = new ArrayList<>(); |
| 190 | + historyEntries.stream().map(HistoryEntryDTO::new).forEach(entries::add); |
| 191 | + return new HistoryDTO(entries, start, count, total); |
| 192 | + } |
| 193 | + |
| 194 | + @GET |
| 195 | + @CorsEnable |
| 196 | + @Produces(MediaType.APPLICATION_JSON) |
| 197 | + public HistoryDTO get(@Context HttpServletRequest request, |
| 198 | + @Context HttpServletResponse response, |
| 199 | + @QueryParam("path") final String path, |
| 200 | + @QueryParam("withFiles") final boolean withFiles, |
| 201 | + @QueryParam("max") @DefaultValue(MAX_RESULTS + "") final int maxEntries, |
| 202 | + @QueryParam("start") @DefaultValue(0 + "") final int startIndex) |
| 203 | + throws HistoryException, IOException { |
| 204 | + |
| 205 | + if (request != null) { |
| 206 | + AuthorizationFramework auth = env.getAuthorizationFramework(); |
| 207 | + if (auth != null) { |
| 208 | + Project p = Project.getProject(path.startsWith("/") ? path : "/" + path); |
| 209 | + if (p != null && !auth.isAllowed(request, p)) { |
| 210 | + response.sendError(Response.status(Response.Status.FORBIDDEN).build().getStatus(), |
| 211 | + "not authorized"); |
| 212 | + return null; |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + History history = HistoryGuru.getInstance().getHistory(new File(env.getSourceRootFile(), path), |
| 218 | + withFiles, true); |
| 219 | + if (history == null) { |
| 220 | + return null; |
| 221 | + } |
| 222 | + |
| 223 | + return getHistoryDTO(history.getHistoryEntries(maxEntries, startIndex), |
| 224 | + startIndex, maxEntries, history.getHistoryEntries().size()); |
| 225 | + } |
| 226 | +} |
0 commit comments