Search in sources :

Example 76 with WebMessage

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;
}
Also used : CurrentUser(org.hisp.dhis.user.CurrentUser) User(org.hisp.dhis.user.User) MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException) ApiToken(org.hisp.dhis.security.apikey.ApiToken) ObjectReport(org.hisp.dhis.feedback.ObjectReport) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 77 with 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()));
}
Also used : DescriptiveWebMessage(org.hisp.dhis.dxf2.webmessage.DescriptiveWebMessage) ExpressionValidationOutcome(org.hisp.dhis.expression.ExpressionValidationOutcome) ExpressionResolver(org.hisp.dhis.analytics.resolver.ExpressionResolver) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 78 with WebMessage

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"));
}
Also used : Category(org.hisp.dhis.category.Category) WebMessage(org.hisp.dhis.dxf2.webmessage.WebMessage) Test(org.junit.jupiter.api.Test)

Aggregations

ResponseBody (org.springframework.web.bind.annotation.ResponseBody)49 PostMapping (org.springframework.web.bind.annotation.PostMapping)29 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)28 InputStream (java.io.InputStream)24 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)20 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)17 PutMapping (org.springframework.web.bind.annotation.PutMapping)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)15 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 JobConfiguration (org.hisp.dhis.scheduling.JobConfiguration)10 User (org.hisp.dhis.user.User)10 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)10 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)9 List (java.util.List)8 Event (org.hisp.dhis.dxf2.events.event.Event)8 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)7 IOException (java.io.IOException)6 FileResourceWebMessageResponse (org.hisp.dhis.dxf2.webmessage.responses.FileResourceWebMessageResponse)6