use of org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer in project alfresco-remote-api by Alfresco.
the class WebScriptOptionsMetaData method writeMetaData.
@Override
public void writeMetaData(OutputStream out, ResourceWithMetadata resource, Map<String, ResourceWithMetadata> allApiResources) throws IOException {
final Object result = processResult(resource, allApiResources);
assistant.getJsonHelper().withWriter(out, new Writer() {
@Override
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
objectMapper.writeValue(generator, result);
}
});
}
use of org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer in project alfresco-remote-api by Alfresco.
the class SerializeTests method writeResponse.
private String writeResponse(final Object respons) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
jsonHelper.withWriter(out, new Writer() {
@Override
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
objectMapper.writeValue(generator, respons);
}
});
System.out.println(out.toString());
return out.toString();
}
use of org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer in project alfresco-remote-api by Alfresco.
the class JsonJacksonTests method testSerializeMultipleObjects.
@Test
public void testSerializeMultipleObjects() throws IOException {
final Collection<Comment> allComments = new ArrayList<Comment>();
Comment aComment = new Comment();
aComment.setContent("<b>There it is</b>");
allComments.add(aComment);
aComment = new Comment();
aComment.setContent("<p>I agree with the author</p>");
allComments.add(aComment);
ByteArrayOutputStream out = new ByteArrayOutputStream();
jsonHelper.withWriter(out, new Writer() {
@Override
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
FilterProvider fp = new SimpleFilterProvider().addFilter(JacksonHelper.DEFAULT_FILTER_NAME, new ReturnAllBeanProperties());
objectMapper.writer(fp).writeValue(generator, allComments);
}
});
assertTrue(out.toString().contains("content\":\"<b>There it is</b>"));
assertTrue(out.toString().contains("content\":\"<p>I agree with the author</p>"));
}
use of org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer in project alfresco-remote-api by Alfresco.
the class InfoWebScriptGet method execute.
@Override
public void execute(final Api api, WebScriptRequest req, WebScriptResponse res) throws IOException {
ResourceDictionary resourceDic = lookupDictionary.getDictionary();
final Map<String, ResourceWithMetadata> apiResources = resourceDic.getAllResources().get(api);
if (apiResources == null) {
throw new InvalidArgumentException(InvalidArgumentException.DEFAULT_INVALID_API);
}
assistant.getJsonHelper().withWriter(res.getOutputStream(), new Writer() {
@Override
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
List<ExecutionResult> entities = new ArrayList<ExecutionResult>();
for (ResourceWithMetadata resource : apiResources.values()) {
entities.add(new ExecutionResult(resource.getMetaData(), null));
}
Collections.sort(entities, new Comparator<ExecutionResult>() {
public int compare(ExecutionResult r1, ExecutionResult r2) {
return ((ResourceMetadata) r1.getRoot()).getUniqueId().compareTo(((ResourceMetadata) r2.getRoot()).getUniqueId());
}
});
objectMapper.writeValue(generator, CollectionWithPagingInfo.asPaged(Paging.DEFAULT, entities));
}
});
}
use of org.alfresco.rest.framework.jacksonextensions.JacksonHelper.Writer in project alfresco-remote-api by Alfresco.
the class NetworkWebScriptGet method execute.
@Override
public void execute(final Api api, final WebScriptRequest req, final WebScriptResponse res) throws IOException {
try {
transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {
@Override
public Void execute() throws Throwable {
// apply content type
res.setContentType(Format.JSON.mimetype() + ";charset=UTF-8");
assistant.getJsonHelper().withWriter(res.getOutputStream(), new Writer() {
@Override
public void writeContents(JsonGenerator generator, ObjectMapper objectMapper) throws JsonGenerationException, JsonMappingException, IOException {
String personId = AuthenticationUtil.getFullyAuthenticatedUser();
String networkId = TenantUtil.getCurrentDomain();
PersonNetwork networkMembership = networks.getNetwork(personId, networkId);
if (networkMembership != null) {
// TODO this is not ideal, but the only way to populate the embedded network entities (this would normally be
// done automatically by the api framework).
Object wrapped = helper.processAdditionsToTheResponse(res, Api.ALFRESCO_PUBLIC, NetworksEntityResource.NAME, Params.valueOf(personId, null, req), networkMembership);
objectMapper.writeValue(generator, wrapped);
} else {
throw new EntityNotFoundException(networkId);
}
}
});
return null;
}
}, true, true);
} catch (ApiException | WebScriptException apiException) {
renderException(apiException, res, assistant);
} catch (RuntimeException runtimeException) {
renderException(runtimeException, res, assistant);
}
}
Aggregations