Skip to content

Commit 590236d

Browse files
authored
add API endpoint for getting file definitions (#4511)
fixes #4508
1 parent 9043272 commit 590236d

File tree

5 files changed

+267
-9
lines changed

5 files changed

+267
-9
lines changed

Diff for: apiary.apib

+84
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,90 @@ The `Content-type` header of the reply will be set accordingly.
145145

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

148+
## File definitions [/file/defs{?path}]
149+
150+
### get file definitions [GET]
151+
152+
+ Parameters
153+
+ path (string) - path of file, relative to source root
154+
155+
+ Response 200 (application/json)
156+
+ Body
157+
158+
[
159+
{
160+
"type": "function",
161+
"signature": "(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
162+
"text": "void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,",
163+
"symbol": "AES_cbc_encrypt",
164+
"lineStart": 5,
165+
"lineEnd": 20,
166+
"line": 20,
167+
"namespace": null
168+
},
169+
{
170+
"type": "argument",
171+
"signature": "(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
172+
"text": "AES_cbc_encrypt(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
173+
"symbol": "in",
174+
"lineStart": 21,
175+
"lineEnd": 44,
176+
"line": 20,
177+
"namespace": null
178+
},
179+
{
180+
"type": "argument",
181+
"signature": "(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
182+
"text": "AES_cbc_encrypt(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
183+
"symbol": "out",
184+
"lineStart": 46,
185+
"lineEnd": 64,
186+
"line": 20,
187+
"namespace": null
188+
},
189+
{
190+
"type": "argument",
191+
"signature": "(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
192+
"text": "AES_cbc_encrypt(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
193+
"symbol": "len",
194+
"lineStart": 21,
195+
"lineEnd": 31,
196+
"line": 21,
197+
"namespace": null
198+
},
199+
{
200+
"type": "argument",
201+
"signature": "(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
202+
"text": "AES_cbc_encrypt(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
203+
"symbol": "key",
204+
"lineStart": 33,
205+
"lineEnd": 51,
206+
"line": 21,
207+
"namespace": null
208+
},
209+
{
210+
"type": "argument",
211+
"signature": "(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
212+
"text": "AES_cbc_encrypt(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
213+
"symbol": "ivec",
214+
"lineStart": 21,
215+
"lineEnd": 40,
216+
"line": 22,
217+
"namespace": null
218+
},
219+
{
220+
"type": "argument",
221+
"signature": "(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
222+
"text": "AES_cbc_encrypt(const unsigned char * in,unsigned char * out,size_t len,const AES_KEY * key,unsigned char * ivec,const int enc)",
223+
"symbol": "enc",
224+
"lineStart": 42,
225+
"lineEnd": 55,
226+
"line": 22,
227+
"namespace": null
228+
}
229+
]
230+
231+
148232
## History [/history{?path,withFiles,start,max}]
149233

150234
### get history entries [GET]

Diff for: opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/Definitions.java

+47-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.analysis;
2525

2626
import org.jetbrains.annotations.Nullable;
27+
import org.opengrok.indexer.util.DTOElement;
2728
import org.opengrok.indexer.util.WhitelistObjectInputFilter;
2829

2930
import java.io.ByteArrayInputStream;
@@ -206,46 +207,91 @@ public static class Tag implements Serializable {
206207

207208
private static final long serialVersionUID = 1217869075425651465L;
208209

210+
public int getLine() {
211+
return line;
212+
}
213+
214+
public String getSymbol() {
215+
return symbol;
216+
}
217+
209218
/**
210219
* Line number of the tag.
211220
*/
221+
@DTOElement
212222
public final int line;
213223
/**
214224
* The symbol used in the definition.
215225
*/
226+
@DTOElement
216227
public final String symbol;
228+
229+
public String getType() {
230+
return type;
231+
}
232+
233+
public String getText() {
234+
return text;
235+
}
236+
237+
public String getNamespace() {
238+
return namespace;
239+
}
240+
241+
public String getSignature() {
242+
return signature;
243+
}
244+
245+
public int getLineStart() {
246+
return lineStart;
247+
}
248+
249+
public int getLineEnd() {
250+
return lineEnd;
251+
}
252+
217253
/**
218254
* The type of the tag.
219255
*/
256+
@DTOElement
220257
public final String type;
221258
/**
222259
* The full line on which the definition occurs.
223260
*/
261+
@DTOElement
224262
public final String text;
225263
/**
226264
* Namespace/class of tag definition.
227265
*/
266+
@DTOElement
228267
public final String namespace;
229268
/**
230269
* Scope of tag definition.
231270
*/
271+
@DTOElement
232272
public final String signature;
233273
/**
234274
* The starting offset (possibly approximate) of {@link #symbol} from
235275
* the start of the line.
236276
*/
277+
@DTOElement
237278
public final int lineStart;
238279
/**
239280
* The ending offset (possibly approximate) of {@link #symbol} from
240281
* the start of the line.
241282
*/
283+
@DTOElement
242284
public final int lineEnd;
243285

244286
/**
245287
* A non-serialized marker for marking a tag to avoid its reuse.
246288
*/
247289
private transient boolean used;
248290

291+
protected Tag() {
292+
this(0, null, null, null, null, null, 0, 0);
293+
}
294+
249295
protected Tag(int line, String symbol, String type, String text,
250296
String namespace, String signature, int lineStart,
251297
int lineEnd) {

Diff for: opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/FileController.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.web.api.v1.controller;
@@ -35,16 +35,23 @@
3535
import org.apache.lucene.document.Document;
3636
import org.apache.lucene.queryparser.classic.ParseException;
3737
import org.opengrok.indexer.analysis.AbstractAnalyzer;
38+
import org.opengrok.indexer.analysis.Definitions;
39+
import org.opengrok.indexer.index.IndexDatabase;
3840
import org.opengrok.indexer.search.QueryBuilder;
3941
import org.opengrok.web.api.v1.filter.CorsEnable;
4042
import org.opengrok.web.api.v1.filter.PathAuthorized;
43+
import org.opengrok.web.util.DTOUtil;
4144
import org.opengrok.web.util.NoPathParameterException;
4245

4346
import java.io.File;
4447
import java.io.FileInputStream;
4548
import java.io.FileNotFoundException;
4649
import java.io.IOException;
4750
import java.io.InputStream;
51+
import java.util.Collection;
52+
import java.util.List;
53+
import java.util.Optional;
54+
import java.util.stream.Collectors;
4855

4956
import static org.opengrok.indexer.index.IndexDatabase.getDocument;
5057
import static org.opengrok.web.util.FileUtil.toFile;
@@ -54,6 +61,8 @@ public class FileController {
5461

5562
public static final String PATH = "file";
5663

64+
65+
5766
private StreamingOutput transfer(File file) throws FileNotFoundException {
5867
if (!file.exists()) {
5968
throw new FileNotFoundException(String.format("file %s does not exist", file));
@@ -141,4 +150,24 @@ public String getGenre(@Context HttpServletRequest request,
141150

142151
return genre.toString();
143152
}
153+
154+
@GET
155+
@CorsEnable
156+
@PathAuthorized
157+
@Path("/defs")
158+
@Produces(MediaType.APPLICATION_JSON)
159+
public List<Object> getDefinitions(@Context HttpServletRequest request,
160+
@Context HttpServletResponse response,
161+
@QueryParam("path") final String path)
162+
throws IOException, NoPathParameterException, ParseException, ClassNotFoundException {
163+
164+
File file = toFile(path);
165+
Definitions defs = IndexDatabase.getDefinitions(file);
166+
return Optional.ofNullable(defs).
167+
map(Definitions::getTags).
168+
stream().
169+
flatMap(Collection::stream).
170+
map(DTOUtil::createDTO).
171+
collect(Collectors.toList());
172+
}
144173
}

0 commit comments

Comments
 (0)