Search in sources :

Example 1 with AutoProxRuleException

use of org.commonjava.indy.autoprox.data.AutoProxRuleException 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;
}
Also used : ResponseUtils.formatResponse(org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) RuleDTO(org.commonjava.indy.autoprox.rest.dto.RuleDTO) AutoProxRuleException(org.commonjava.indy.autoprox.data.AutoProxRuleException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with AutoProxRuleException

use of org.commonjava.indy.autoprox.data.AutoProxRuleException in project indy by Commonjava.

the class AutoProxCatalogResource method reparseCatalog.

@ApiOperation("Reparse the AutoProx rule catalog from files.")
@ApiResponse(code = 200, message = "Re-parsing is complete.")
@DELETE
public Response reparseCatalog() {
    Response response = checkEnabled();
    if (response != null) {
        return response;
    }
    try {
        controller.reparseCatalog();
        response = Response.ok().build();
    } catch (final AutoProxRuleException e) {
        logger.error(String.format("Failed to reparse catalog from disk. Reason: %s", e.getMessage()), e);
        response = formatResponse(e);
    }
    return response;
}
Also used : ResponseUtils.formatResponse(org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) AutoProxRuleException(org.commonjava.indy.autoprox.data.AutoProxRuleException) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponse(io.swagger.annotations.ApiResponse)

Example 3 with AutoProxRuleException

use of org.commonjava.indy.autoprox.data.AutoProxRuleException 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;
}
Also used : ResponseUtils.formatResponse(org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) RuleDTO(org.commonjava.indy.autoprox.rest.dto.RuleDTO) IOException(java.io.IOException) URI(java.net.URI) AutoProxRuleException(org.commonjava.indy.autoprox.data.AutoProxRuleException) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponse(io.swagger.annotations.ApiResponse)

Example 4 with AutoProxRuleException

use of org.commonjava.indy.autoprox.data.AutoProxRuleException 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;
}
Also used : ResponseUtils.formatResponse(org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) RuleDTO(org.commonjava.indy.autoprox.rest.dto.RuleDTO) IOException(java.io.IOException) URI(java.net.URI) AutoProxRuleException(org.commonjava.indy.autoprox.data.AutoProxRuleException) Path(javax.ws.rs.Path) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) PUT(javax.ws.rs.PUT)

Example 5 with AutoProxRuleException

use of org.commonjava.indy.autoprox.data.AutoProxRuleException in project indy by Commonjava.

the class ScriptRuleParser method parseRule.

public RuleMapping parseRule(final String spec, final String scriptName) throws AutoProxRuleException {
    if (spec == null) {
        return null;
    }
    AutoProxRule rule;
    try {
        rule = scriptEngine.parseScriptInstance(spec, AutoProxRule.class);
    } catch (final IndyGroovyException e) {
        throw new AutoProxRuleException("[AUTOPROX] Cannot load autoprox factory from: {} as an instance of: {}. Reason: {}", e, scriptName, AutoProxRule.class.getSimpleName(), e.getMessage());
    }
    if (rule != null) {
        return new RuleMapping(FilenameUtils.removeExtension(scriptName), spec, rule);
    }
    logger.warn("Rule named: {} parsed to null AutoProxRule instance. Spec was:\n\n{}\n\n", scriptName, spec);
    return null;
}
Also used : AutoProxRule(org.commonjava.indy.autoprox.data.AutoProxRule) IndyGroovyException(org.commonjava.indy.subsys.template.IndyGroovyException) RuleMapping(org.commonjava.indy.autoprox.data.RuleMapping) AutoProxRuleException(org.commonjava.indy.autoprox.data.AutoProxRuleException)

Aggregations

AutoProxRuleException (org.commonjava.indy.autoprox.data.AutoProxRuleException)5 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponse (io.swagger.annotations.ApiResponse)4 Response (javax.ws.rs.core.Response)4 ResponseUtils.formatResponse (org.commonjava.indy.bind.jaxrs.util.ResponseUtils.formatResponse)4 RuleDTO (org.commonjava.indy.autoprox.rest.dto.RuleDTO)3 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)2 ApiResponses (io.swagger.annotations.ApiResponses)2 IOException (java.io.IOException)2 URI (java.net.URI)2 Consumes (javax.ws.rs.Consumes)2 DELETE (javax.ws.rs.DELETE)2 Path (javax.ws.rs.Path)2 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 AutoProxRule (org.commonjava.indy.autoprox.data.AutoProxRule)1 RuleMapping (org.commonjava.indy.autoprox.data.RuleMapping)1 IndyGroovyException (org.commonjava.indy.subsys.template.IndyGroovyException)1