use of org.restlet.representation.Representation in project pinot by linkedin.
the class PinotSegmentUploadRestletResource method getSegmentFile.
@HttpVerb("get")
@Summary("Downloads a segment")
@Tags({ "segment", "table" })
@Paths({ "/segments/{tableName}/{segmentName}" })
@Responses({ @Response(statusCode = "200", description = "A segment file in Pinot format"), @Response(statusCode = "404", description = "The segment file or table does not exist") })
private Representation getSegmentFile(@Parameter(name = "tableName", in = "path", description = "The name of the table in which the segment resides", required = true) String tableName, @Parameter(name = "segmentName", in = "path", description = "The name of the segment to download", required = true) String segmentName) {
Representation presentation;
try {
segmentName = URLDecoder.decode(segmentName, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 encoding should always be supported", e);
}
final File dataFile = new File(baseDataDir, StringUtil.join("/", tableName, segmentName));
if (dataFile.exists()) {
presentation = new FileRepresentation(dataFile, MediaType.ALL, 0);
} else {
setStatus(Status.CLIENT_ERROR_NOT_FOUND);
presentation = new StringRepresentation("Table or segment is not found!");
}
return presentation;
}
use of org.restlet.representation.Representation in project pinot by linkedin.
the class PinotSchemaRestletResource method getUploadContents.
private File getUploadContents() throws Exception {
File dataFile = null;
// 1/ Create a factory for disk-based file items
Representation rep;
final DiskFileItemFactory factory = new DiskFileItemFactory();
// 2/ Create a new file upload handler based on the Restlet
// FileUpload extension that will parse Restlet requests and
// generates FileItems.
final RestletFileUpload upload = new RestletFileUpload(factory);
final List<FileItem> items;
// 3/ Request is parsed by the handler which generates a
// list of FileItems
items = upload.parseRequest(getRequest());
final Iterator<FileItem> it = items.iterator();
while (it.hasNext() && dataFile == null) {
final FileItem fi = it.next();
if (fi.getFieldName() != null) {
dataFile = new File(tempDir, fi.getFieldName() + "-" + System.currentTimeMillis());
fi.write(dataFile);
}
}
return dataFile;
}
use of org.restlet.representation.Representation in project pinot by linkedin.
the class LLCSegmentCommitTest method testSegmentCommit.
@Test
public void testSegmentCommit() throws Exception {
SegmentCompletionManager.create(createMockHelixManager(), null, new ControllerConf(), new ControllerMetrics(new MetricsRegistry()));
FakeLLCSegmentCommit segmentCommit = new FakeLLCSegmentCommit();
Representation representation;
String strResponse;
JSONObject jsonResponse;
// If commitStart returns failed, upload should not be called, and the client should get 'failed'
segmentCommit._uploadCalled = false;
segmentCommit._scm.commitStartResponse = SegmentCompletionProtocol.RESP_FAILED;
representation = segmentCommit.post(null);
strResponse = representation.getText();
jsonResponse = JSONObject.parseObject(strResponse);
Assert.assertEquals(jsonResponse.get(SegmentCompletionProtocol.STATUS_KEY), SegmentCompletionProtocol.ControllerResponseStatus.FAILED.toString());
Assert.assertFalse(segmentCommit._uploadCalled);
// if commitStart returns continue, and upload fails, then commitEnd should indicate upload failure.
segmentCommit._uploadCalled = false;
segmentCommit._scm.commitStartResponse = SegmentCompletionProtocol.RESP_COMMIT_CONTINUE;
segmentCommit._scm.commitEndResponse = SegmentCompletionProtocol.RESP_FAILED;
segmentCommit._uploadReturnValue = false;
representation = segmentCommit.post(null);
strResponse = representation.getText();
jsonResponse = JSONObject.parseObject(strResponse);
Assert.assertEquals(jsonResponse.get(SegmentCompletionProtocol.STATUS_KEY), SegmentCompletionProtocol.ControllerResponseStatus.FAILED.toString());
Assert.assertTrue(segmentCommit._uploadCalled);
Assert.assertFalse(segmentCommit._scm.uploadSuccess);
// If commitstart returns CONINUE and upload succeeds, we should return whatever commitEnd returns.
segmentCommit._uploadCalled = false;
segmentCommit._scm.commitStartResponse = SegmentCompletionProtocol.RESP_COMMIT_CONTINUE;
segmentCommit._scm.commitEndResponse = SegmentCompletionProtocol.RESP_COMMIT_SUCCESS;
segmentCommit._uploadReturnValue = true;
representation = segmentCommit.post(null);
strResponse = representation.getText();
jsonResponse = JSONObject.parseObject(strResponse);
Assert.assertEquals(jsonResponse.get(SegmentCompletionProtocol.STATUS_KEY), SegmentCompletionProtocol.ControllerResponseStatus.COMMIT_SUCCESS.toString());
Assert.assertTrue(segmentCommit._uploadCalled);
Assert.assertTrue(segmentCommit._scm.uploadSuccess);
}
use of org.restlet.representation.Representation in project OpenAM by OpenRock.
the class AbstractRestletAccessAuditFilterTest method shouldCallHandleOnRestlet.
@Test
public void shouldCallHandleOnRestlet() {
// Given
Request request = mock(Request.class);
Response response = new Response(request);
Representation representation = mock(Representation.class);
when(request.getEntity()).thenReturn(representation);
when(request.getAttributes()).thenReturn(new ConcurrentHashMap<String, Object>());
when(representation.isTransient()).thenReturn(false);
when(eventPublisher.isAuditing(anyString(), anyString(), any(EventName.class))).thenReturn(false);
// When
auditFilter.handle(request, response);
// Then
verify(restlet, times(1)).handle(any(Request.class), any(Response.class));
}
use of org.restlet.representation.Representation in project Xponents by OpenSextant.
the class XponentsGeotagger method format.
private Representation format(List<TextMatch> matches, RequestParameters jobParams) throws JSONException {
Representation result = null;
int tagCount = 0;
JSONObject resultContent = new JSONObject();
JSONObject resultMeta = new JSONObject();
resultMeta.put("status", "ok");
resultMeta.put("numfound", 0);
JSONArray resultArray = new JSONArray();
/*
* Super loop: Iterate through all found entities. record Taxons as
* person or orgs record Geo tags as country, place, or geo. geo =
* geocoded place or parsed coordinate (MGRS, DMS, etc)
*
*/
for (TextMatch name : matches) {
/*
* ==========================
* ANNOTATIONS: non-geographic entities that are filtered out, but worth tracking
* ==========================
*/
if (name instanceof TaxonMatch) {
if (jobParams.output_taxons) {
TaxonMatch match = (TaxonMatch) name;
++tagCount;
for (Taxon n : match.getTaxons()) {
JSONObject node = populateMatch(name);
String t = "taxon";
String taxon_name = n.name.toLowerCase();
if (taxon_name.startsWith("org.")) {
t = "org";
} else if (taxon_name.startsWith("person.")) {
t = "person";
}
node.put("type", t);
// Name of taxon
node.put("taxon", n.name);
// Name of catalog or source
node.put("catalog", n.catalog);
// node.put("filtered-out", true);
resultArray.put(node);
break;
}
}
continue;
}
// Ignore non-place tags
if (name.isFilteredOut() || !(name instanceof PlaceCandidate || name instanceof GeocoordMatch)) {
continue;
}
JSONObject node = populateMatch(name);
/*
* ==========================
* ANNOTATIONS: coordinates
* ==========================
*/
if (name instanceof GeocoordMatch) {
++tagCount;
GeocoordMatch geo = (GeocoordMatch) name;
node.put("type", "coordinate");
Transforms.createGeocoding(geo, node);
resultArray.put(node);
continue;
}
if (name.isFilteredOut()) {
debug("Filtered out " + name.getText());
continue;
}
PlaceCandidate place = (PlaceCandidate) name;
Place resolvedPlace = place.getChosen();
/*
* ==========================
* ANNOTATIONS: countries, places, etc.
* ==========================
*/
/*
* Accept all country names as potential geotags Else if name can be
* filtered out, do it now. Otherwise it is a valid place name to
* consider
*/
++tagCount;
if (place.isCountry) {
node.put("name", resolvedPlace.getPlaceName());
node.put("type", "country");
node.put("cc", resolvedPlace.getCountryCode());
node.put("confidence", place.getConfidence());
} else {
/*
* Conf = 20 or greater to be geocoded.
*/
Transforms.createGeocoding(resolvedPlace, node);
node.put("name", resolvedPlace.getPlaceName());
node.put("type", "place");
node.put("confidence", place.getConfidence());
if (place.getConfidence() <= 10) {
node.put("filtered-out", true);
}
}
resultArray.put(node);
}
resultMeta.put("numfound", tagCount);
resultContent.put("response", resultMeta);
resultContent.put("annotations", resultArray);
result = new JsonRepresentation(resultContent.toString(2));
result.setCharacterSet(CharacterSet.UTF_8);
return result;
}
Aggregations