use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class ApiTokenController method postJsonObject.
@PostMapping(consumes = "application/json")
@ResponseBody
public WebMessage postJsonObject(HttpServletRequest request) throws Exception {
final ApiToken apiToken = deserializeJsonEntity(request);
User user = currentUserService.getCurrentUser();
if (!aclService.canCreate(user, getEntityClass())) {
throw new CreateAccessDeniedException("You don't have the proper permissions to create this object.");
}
apiToken.getTranslations().clear();
// Validate input values is ok
validateBeforeCreate(apiToken);
// We only make personal access tokens for now
apiToken.setType(ApiTokenType.PERSONAL_ACCESS_TOKEN);
// Generate key and set default values
apiTokenService.initToken(apiToken);
// Save raw key to send in response
final String rawKey = apiToken.getKey();
// Hash the raw token key and overwrite value in the entity to persist
final String hashedKey = apiTokenService.hashKey(apiToken.getKey());
apiToken.setKey(hashedKey);
// Continue POST import as usual
MetadataImportParams params = importService.getParamsFromMap(contextService.getParameterValuesMap()).setImportReportMode(ImportReportMode.FULL).setUser(user).setImportStrategy(ImportStrategy.CREATE).addObject(apiToken);
final ObjectReport objectReport = importService.importMetadata(params).getFirstObjectReport();
final String uid = objectReport.getUid();
WebMessage webMessage = objectReport(objectReport);
if (webMessage.getStatus() == Status.OK) {
webMessage.setHttpStatus(HttpStatus.CREATED);
webMessage.setLocation(getSchema().getRelativeApiEndpoint() + "/" + uid);
// Set our custom web response object that includes the new
// generated key.
webMessage.setResponse(new ApiTokenCreationResponse(objectReport, rawKey));
} else {
webMessage.setStatus(Status.ERROR);
}
return webMessage;
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class IndicatorController method getExpressionDescription.
@PostMapping(value = "/expression/description", produces = APPLICATION_JSON_VALUE)
@ResponseBody
public WebMessage getExpressionDescription(@RequestBody String expression) {
String resolvingExpression = expression;
for (ExpressionResolver resolver : resolvers.getExpressionResolvers()) {
resolvingExpression = resolver.resolve(resolvingExpression);
}
String resolvedExpression = resolvingExpression;
ExpressionValidationOutcome result = expressionService.expressionIsValid(resolvedExpression, INDICATOR_EXPRESSION);
return new DescriptiveWebMessage(result.isValid() ? Status.OK : Status.ERROR, HttpStatus.OK).setDescription(result::isValid, () -> expressionService.getExpressionDescription(resolvedExpression, INDICATOR_EXPRESSION)).setMessage(i18nManager.getI18n().getString(result.getKey()));
}
use of org.hisp.dhis.dxf2.webmessage.WebMessage in project dhis2-core by dhis2.
the class SharingControllerTest method systemDefaultMetadata.
@Test
void systemDefaultMetadata() throws Exception {
final Category category = new Category();
category.setName(Category.DEFAULT_NAME);
doReturn(Category.class).when(aclService).classForType(eq("category"));
when(aclService.isClassShareable(eq(Category.class))).thenReturn(true);
when(manager.getNoAcl(eq(Category.class), eq("kkSjhdhks"))).thenReturn(category);
WebMessage message = sharingController.postSharing("category", "kkSjhdhks", request);
assertThat(message.getMessage(), containsString("Sharing settings of system default metadata object"));
}
Aggregations