-
Notifications
You must be signed in to change notification settings - Fork 20
/
TextResource.java
198 lines (164 loc) · 4.83 KB
/
TextResource.java
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package com.ipeirotis.readability.web.v1;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import com.google.common.collect.Lists;
import com.ipeirotis.readability.model.Text;
import com.ipeirotis.readability.model.TextMetric;
import com.ipeirotis.readability.service.ReadabilityService;
import com.ipeirotis.readability.service.TextMetricService;
import com.ipeirotis.readability.service.TextService;
/**
* Represents the Text Manipulation Resource
*
* @author aldrin
*
*/
@Component
@Scope("request")
@Produces(MediaType.APPLICATION_JSON_VALUE)
@Path("/v1/text")
public class TextResource extends BaseResource {
@Autowired
TextService textService;
@Autowired
TextMetricService textMetricService;
@Autowired
ReadabilityService readabilityService;
/**
* Handler needed for CORS support
*
* @return CORS Response
*/
@OPTIONS
@Path(".*")
public Response getOptionsForUpdateText() throws Exception {
return Response
.ok()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods",
"GET, POST, DELETE, OPTIONS")
.header("Access-Control-Allow-Headers", "x-requested-with")
.build();
}
/**
* Updates (read: stores) a Text
*
* @param contents
* Body Context (Form Method)
* @return Created Text Object
*/
@POST
public Text updateText(String contents) throws Exception {
@SuppressWarnings("unused")
Text o = null, t = null;
if (contents.startsWith("body=")) {
contents = contents.substring("body=".length());
}
t = new Text(user, contents);
if (null == (o = textService.findFirstByPrimaryKey(t.getId()))) {
textService.save(t);
textMetricService.save(readabilityService.getMetrics(t));
}
return t;
}
/**
* Given an Id, returns the Text
*
* @param id
* id, in the form <userid>:<sha1hash>
*
* @return Text Object
*/
@Path("/{id : [^/]+}")
@GET
public Text findById(@PathParam("id") String id) throws Exception {
Text t = textService.findFirstByPrimaryKey(id);
if (null == t || (!t.getOwner().equals(user.getId())))
throw new WebApplicationException(404);
return t;
}
/**
* Returns the text metrics for given id
*
* @param id
* id, in the form <userid>:<sha1hash>
* @return Json Array of Object Id + Metrics
*/
@Path("/{id : [^/]+}/metrics")
@GET
public Map<String, Object> findMetricsById(@PathParam("id") String id)
throws Exception {
Text t = findById(id);
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("id", t.getId());
Map<String, BigDecimal> metrics = new LinkedHashMap<String, BigDecimal>();
for (TextMetric m : textMetricService.findAllByParentKey(t.getId())) {
metrics.put(m.getMetricType(), m.getValue());
}
result.put("metrics", metrics);
return result;
}
/**
* Returns a Named Metric
*
* @param id
* id, in the form <userid>:<sha1hash>
* @param metricType
* metric type (ARI, CHARACTERS, SYLLABLES, ...)
* @return Named Metric (just like the example in findMetricsById
*/
@Path("/{id : [^/]+}/metrics/{metricType : \\w+}")
@GET
public Map<String, Object> findNamedMetricsById(@PathParam("id") String id,
@PathParam("metricType") String metricType) throws Exception {
Text t = findById(id);
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put("id", t.getId());
Map<String, BigDecimal> metrics = new LinkedHashMap<String, BigDecimal>();
TextMetric m = textMetricService.findFirstByPrimaryKey(String.format(
"%s:%s", t.getId(), metricType));
metrics.put(m.getMetricType(), m.getValue());
result.put("metrics", metrics);
return result;
}
/**
* Deletes a Text
*
* @param id
* id, in the form <userid>:<sha1hash>
* @return Text Data, prior to removal
*/
@Path("/{id : [^/]+}")
@DELETE
public Text deleteById(@PathParam("id") String id) throws Exception {
Text t = textService.findFirstByPrimaryKey(id);
if (null == t || (t.getOwner() != user.getId()))
throw new WebApplicationException(404);
textService.delete(t);
return t;
}
/**
* Returns all texts which belongs to this user
*
* @return array of Text Objects
*/
@GET
public Collection<Text> findAll() {
return Lists.newArrayList(textService.findAllByParentKey(user.getId()));
}
}