use of org.commonjava.indy.autoprox.rest.dto.RuleDTO in project indy by Commonjava.
the class AbstractAutoproxCatalogTest method getRule.
protected RuleDTO getRule(final String named, final String ruleScriptResource) throws IOException {
final URL resource = Thread.currentThread().getContextClassLoader().getResource(ruleScriptResource);
if (resource == null) {
Assert.fail("Cannot find classpath resource: " + ruleScriptResource);
}
final String spec = IOUtils.toString(resource);
return new RuleDTO(named, spec);
}
use of org.commonjava.indy.autoprox.rest.dto.RuleDTO in project indy by Commonjava.
the class AutoProxCatalogResource method deleteRule.
@ApiOperation(value = "Delete a single AutoProx rule")
@ApiResponses({ @ApiResponse(code = 404, message = "No rule by the specified name"), @ApiResponse(code = 204, message = "Rule spec deleted") })
@DELETE
@Path("{name}")
public Response deleteRule(@PathParam("name") final String name, @Context final HttpServletRequest request, @Context final SecurityContext securityContext) {
Response response = checkEnabled();
if (response != null) {
return response;
}
try {
String user = securityManager.getUser(securityContext, request);
final RuleDTO dto = controller.deleteRule(name, user);
if (dto == null) {
response = status(NOT_FOUND).build();
} else {
response = noContent().build();
}
} catch (final AutoProxRuleException e) {
logger.error(String.format("Failed to delete rule: %s. Reason: %s", name, e.getMessage()), e);
response = formatResponse(e);
}
return response;
}
use of org.commonjava.indy.autoprox.rest.dto.RuleDTO in project indy by Commonjava.
the class AutoProxCatalogResource method createRule.
@ApiOperation(value = "Create an AutoProx rule", response = RuleDTO.class)
@ApiResponse(code = 201, message = "Rule created", responseHeaders = @ResponseHeader(name = "Location", description = "Resource location of the new rule"))
@ApiImplicitParams({ @ApiImplicitParam(allowMultiple = false, paramType = "body", name = "body", required = true, dataType = "org.commonjava.indy.autoprox.rest.dto.RuleDTO", value = "The rule definition JSON") })
@POST
@Consumes(ApplicationContent.application_json)
public Response createRule(@Context final HttpServletRequest request, @Context final UriInfo uriInfo, @Context SecurityContext securityContext) {
Response response = checkEnabled();
if (response != null) {
return response;
}
RuleDTO dto = null;
try {
dto = serializer.readValue(request.getInputStream(), RuleDTO.class);
} catch (final IOException e) {
final String message = "Failed to read " + RuleDTO.class.getSimpleName() + " from request body.";
logger.error(message, e);
response = formatResponse(e, message);
}
if (response != null) {
return response;
}
try {
String user = securityManager.getUser(securityContext, request);
dto = controller.storeRule(dto, user);
final URI uri = uriInfo.getBaseUriBuilder().path(getClass()).path(dto.getName()).build();
response = formatCreatedResponseWithJsonEntity(uri, dto, serializer);
} catch (final AutoProxRuleException e) {
final String message = "Failed to store rule: " + dto.getName() + ".";
logger.error(message, e);
response = formatResponse(e, message);
}
return response;
}
use of org.commonjava.indy.autoprox.rest.dto.RuleDTO in project indy by Commonjava.
the class AutoProxCatalogResource method updateRule.
@ApiOperation(value = "Update an AutoProx rule", response = RuleDTO.class)
@ApiResponses({ @ApiResponse(code = 201, message = "Rule created", responseHeaders = @ResponseHeader(name = "Location", description = "Resource location of the new rule")), @ApiResponse(code = 200, message = "Existing rule updated") })
@ApiImplicitParams({ @ApiImplicitParam(allowMultiple = false, paramType = "body", name = "body", required = true, dataType = "org.commonjava.indy.autoprox.rest.dto.RuleDTO", value = "The rule definition JSON (NOTE: Name will over OVERRIDDEN with value from storage path.)") })
@PUT
@Path("{name}")
@Consumes(ApplicationContent.application_json)
public Response updateRule(@PathParam("name") final String name, @Context final HttpServletRequest request, @Context final UriInfo uriInfo, @Context final SecurityContext securityContext) {
Response response = checkEnabled();
if (response != null) {
return response;
}
RuleDTO dto = controller.getRule(name);
boolean updating = true;
if (dto == null) {
updating = false;
}
try {
dto = serializer.readValue(request.getInputStream(), RuleDTO.class);
dto.setName(name);
} catch (final IOException e) {
final String message = "Failed to read " + RuleDTO.class.getSimpleName() + " from request body.";
logger.error(message, e);
response = formatResponse(e, message);
}
if (response != null) {
return response;
}
try {
String user = securityManager.getUser(securityContext, request);
dto = controller.storeRule(dto, user);
if (updating) {
response = ok().build();
} else {
final URI uri = uriInfo.getBaseUriBuilder().path(getClass()).path(dto.getName()).build();
response = created(uri).build();
}
} catch (final AutoProxRuleException e) {
final String message = "Failed to store rule: " + dto.getName() + ".";
logger.error(message, e);
response = formatResponse(e, message);
}
return response;
}
use of org.commonjava.indy.autoprox.rest.dto.RuleDTO in project indy by Commonjava.
the class CreateAndDeleteRuleTest method createRuleDeleteAndVerifyMissing.
@Test
public void createRuleDeleteAndVerifyMissing() throws Exception {
final CatalogDTO catalog = module.getCatalog();
assertThat(catalog.isEnabled(), equalTo(true));
assertThat(catalog.getRules().isEmpty(), equalTo(true));
final RuleDTO rule = getRule("0001-simple-rule", "rules/simple-rule.groovy");
RuleDTO dto = module.storeRule(rule);
assertThat(dto, notNullValue());
assertThat(dto, equalTo(rule));
module.deleteRuleNamed(dto.getName());
dto = module.getRuleNamed(dto.getName());
assertThat(dto, nullValue());
}
Aggregations