Search in sources :

Example 1 with OpenItemDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenItemDTO in project apollo by ctripcorp.

the class ItemController method updateItem.

@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key:.+}", method = RequestMethod.PUT)
public void updateItem(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable String key, @RequestBody OpenItemDTO item, HttpServletRequest request) {
    RequestPrecondition.checkArguments(item != null, "item payload can not be empty");
    RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(item.getKey(), item.getValue(), item.getDataChangeLastModifiedBy()), "key,value,dataChangeLastModifiedBy can not be empty");
    RequestPrecondition.checkArguments(item.getKey().equals(key), "Key in path and payload is not consistent");
    if (userService.findByUserId(item.getDataChangeLastModifiedBy()) == null) {
        throw new BadRequestException("user(dataChangeLastModifiedBy) not exists");
    }
    ItemDTO toUpdateItem = itemService.loadItem(Env.fromString(env), appId, clusterName, namespaceName, item.getKey());
    if (toUpdateItem == null) {
        throw new BadRequestException("item not exists");
    }
    // protect. only value,comment,lastModifiedBy can be modified
    toUpdateItem.setComment(item.getComment());
    toUpdateItem.setValue(item.getValue());
    toUpdateItem.setDataChangeLastModifiedBy(item.getDataChangeLastModifiedBy());
    itemService.updateItem(appId, Env.fromString(env), clusterName, namespaceName, toUpdateItem);
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with OpenItemDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenItemDTO in project apollo by ctripcorp.

the class ServerItemOpenApiService method createItem.

@Override
public OpenItemDTO createItem(String appId, String env, String clusterName, String namespaceName, OpenItemDTO itemDTO) {
    ItemDTO toCreate = OpenApiBeanUtils.transformToItemDTO(itemDTO);
    // protect
    toCreate.setLineNum(0);
    toCreate.setId(0);
    toCreate.setDataChangeLastModifiedBy(toCreate.getDataChangeCreatedBy());
    toCreate.setDataChangeLastModifiedTime(null);
    toCreate.setDataChangeCreatedTime(null);
    ItemDTO createdItem = itemService.createItem(appId, Env.valueOf(env), clusterName, namespaceName, toCreate);
    return OpenApiBeanUtils.transformFromItemDTO(createdItem);
}
Also used : OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO)

Example 3 with OpenItemDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenItemDTO in project apollo by ctripcorp.

the class ItemOpenApiServiceTest method testCreateItemWithError.

@Test(expected = RuntimeException.class)
public void testCreateItemWithError() throws Exception {
    String someKey = "someKey";
    String someValue = "someValue";
    String someCreatedBy = "someCreatedBy";
    OpenItemDTO itemDTO = new OpenItemDTO();
    itemDTO.setKey(someKey);
    itemDTO.setValue(someValue);
    itemDTO.setDataChangeCreatedBy(someCreatedBy);
    when(statusLine.getStatusCode()).thenReturn(400);
    itemOpenApiService.createItem(someAppId, someEnv, someCluster, someNamespace, itemDTO);
}
Also used : OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) Test(org.junit.Test)

Example 4 with OpenItemDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenItemDTO in project apollo by ctripcorp.

the class ItemOpenApiServiceTest method testUpdateItemWithError.

@Test(expected = RuntimeException.class)
public void testUpdateItemWithError() throws Exception {
    String someKey = "someKey";
    String someValue = "someValue";
    String someModifiedBy = "someModifiedBy";
    OpenItemDTO itemDTO = new OpenItemDTO();
    itemDTO.setKey(someKey);
    itemDTO.setValue(someValue);
    itemDTO.setDataChangeLastModifiedBy(someModifiedBy);
    when(statusLine.getStatusCode()).thenReturn(400);
    itemOpenApiService.updateItem(someAppId, someEnv, someCluster, someNamespace, itemDTO);
}
Also used : OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) Test(org.junit.Test)

Example 5 with OpenItemDTO

use of com.ctrip.framework.apollo.openapi.dto.OpenItemDTO in project apollo by ctripcorp.

the class ItemController method createItem.

@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
public OpenItemDTO createItem(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @RequestBody OpenItemDTO item, HttpServletRequest request) {
    RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(item.getKey(), item.getValue(), item.getDataChangeCreatedBy()), "key,value,dataChangeCreatedBy 字段不能为空");
    if (userService.findByUserId(item.getDataChangeCreatedBy()) == null) {
        throw new BadRequestException("用户不存在.");
    }
    ItemDTO toCreate = OpenApiBeanUtils.transformToItemDTO(item);
    // protect
    toCreate.setLineNum(0);
    toCreate.setId(0);
    toCreate.setDataChangeLastModifiedBy(toCreate.getDataChangeCreatedBy());
    toCreate.setDataChangeLastModifiedTime(null);
    toCreate.setDataChangeCreatedTime(null);
    ItemDTO createdItem = itemService.createItem(appId, Env.fromString(env), clusterName, namespaceName, toCreate);
    return OpenApiBeanUtils.transformFromItemDTO(createdItem);
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)11 Test (org.junit.Test)6 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)4 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)2 HttpPut (org.apache.http.client.methods.HttpPut)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 OpenNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO)1 ItemBO (com.ctrip.framework.apollo.portal.entity.bo.ItemBO)1 LinkedList (java.util.LinkedList)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1