blob: f50e7c247b1fdc6fe9104a1d2effc010333daca3 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.restapi;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import static com.yahoo.jdisc.Response.Status.*;
/**
* Represents the request URI with its values.
*
* @author dybis
*/
public class RestUri {
public static final char NUMBER_STREAMING = 'n';
public static final char GROUP_STREAMING = 'g';
public static final String DOCUMENT = "document";
public static final String V_1 = "v1";
public static final String ID = "id:";
/**
* Represents the "grouping" part of document id which can be used with streaming model.
*/
public static class Group {
public final char name;
public final String value;
Group(char name, String value) {
this.name = name;
this.value = value;
}
}
private final String namespace;
private final String documentType;
private final String docId;
private Optional<Group> group = Optional.empty();
private final String rawPath;
public String getRawPath() {
return rawPath;
}
public String getNamespace() {
return namespace;
}
public String getDocumentType() {
return documentType;
}
public String getDocId() {
return docId;
}
public Optional<Group> getGroup() {
return group;
}
public String generateFullId() {
return ID + namespace + ":" + documentType + ":"
+ (getGroup().isPresent() ? group.get().name + "=" + group.get().value : "")
+ ":" + docId;
}
static class PathParser {
final List<String> rawParts;
final String originalPath;
int readPos = 0;
public PathParser(String path) {
this.originalPath = path;
this.rawParts = Splitter.on('/').splitToList(path);
}
String nextTokenOrException() throws RestApiException {
if (readPos >= rawParts.size()) {
throwUsage(originalPath);
}
return rawParts.get(readPos++);
}
String restOfPath() throws RestApiException {
String rawId = Joiner.on("/").join(rawParts.listIterator(readPos));
try {
return URLDecoder.decode(rawId, StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RestApiException(Response.createErrorResponse(BAD_REQUEST,"Problems decoding the URI: " + e.getMessage()));
}
}
}
public RestUri(URI uri) throws RestApiException {
rawPath = uri.getRawPath();
PathParser pathParser = new PathParser(rawPath);
if (! pathParser.nextTokenOrException().equals("") ||
! pathParser.nextTokenOrException().equals(DOCUMENT) ||
! pathParser.nextTokenOrException().equals(V_1)) {
throwUsage(uri.getRawPath());
}
namespace = pathParser.nextTokenOrException();
documentType = pathParser.nextTokenOrException();
switch (pathParser.nextTokenOrException()) {
case "number":
group = Optional.of(new Group(NUMBER_STREAMING, pathParser.nextTokenOrException()));
break;
case "docid": group = Optional.empty();
break;
case "group":
group = Optional.of(new Group(GROUP_STREAMING, pathParser.nextTokenOrException()));
break;
default: throwUsage(uri.getRawPath());
}
docId = pathParser.restOfPath();
}
private static void throwUsage(String inputPath) throws RestApiException {
throw new RestApiException(Response.createErrorResponse(BAD_REQUEST,
"Expected:\n" +
".../{namespace}/{document-type}/group/{name}/[{user-specified}]\n" +
".../{namespace}/{document-type}/docid/[{user-specified}]\n: but got " + inputPath));
}
}
|