Search in sources :

Example 46 with ResponseEntity

use of org.springframework.http.ResponseEntity in project cuba by cuba-platform.

the class EntitiesController method searchEntitiesListGet.

@GetMapping("/{entityName}/search")
public ResponseEntity<String> searchEntitiesListGet(@PathVariable String entityName, @RequestParam String filter, @RequestParam(required = false) String view, @RequestParam(required = false) Integer limit, @RequestParam(required = false) Integer offset, @RequestParam(required = false) String sort, @RequestParam(required = false) Boolean returnNulls, @RequestParam(required = false) Boolean returnCount, @RequestParam(required = false) Boolean dynamicAttributes, @RequestParam(required = false) String modelVersion) {
    EntitiesSearchResult entitiesSearchResult = entitiesControllerManager.searchEntities(entityName, filter, view, limit, offset, sort, returnNulls, returnCount, dynamicAttributes, modelVersion);
    ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.status(HttpStatus.OK);
    if (BooleanUtils.isTrue(returnCount)) {
        responseBuilder.header("X-Total-Count", entitiesSearchResult.getCount().toString());
    }
    return responseBuilder.body(entitiesSearchResult.getJson());
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) EntitiesSearchResult(com.haulmont.restapi.data.EntitiesSearchResult)

Example 47 with ResponseEntity

use of org.springframework.http.ResponseEntity in project cuba by cuba-platform.

the class FileUploadController method createFileInfoResponseEntity.

protected ResponseEntity<FileInfo> createFileInfoResponseEntity(HttpServletRequest request, FileDescriptor fd) {
    FileInfo fileInfo = new FileInfo(fd.getId(), fd.getName(), fd.getSize());
    UriComponents uriComponents = UriComponentsBuilder.fromHttpUrl(request.getRequestURL().toString()).path("/{id}").buildAndExpand(fd.getId().toString());
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setLocation(uriComponents.toUri());
    return new ResponseEntity<>(fileInfo, httpHeaders, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) UriComponents(org.springframework.web.util.UriComponents) ResponseEntity(org.springframework.http.ResponseEntity) FileInfo(com.haulmont.restapi.data.FileInfo)

Example 48 with ResponseEntity

use of org.springframework.http.ResponseEntity in project cuba by cuba-platform.

the class RestControllerExceptionHandler method handleMethodResultValidationException.

@ExceptionHandler(MethodResultValidationException.class)
@ResponseBody
public ResponseEntity<ErrorInfo> handleMethodResultValidationException(MethodResultValidationException e) {
    log.error("MethodResultValidationException in service", e);
    ErrorInfo errorInfo = new ErrorInfo("Server error", "");
    return new ResponseEntity<>(errorInfo, HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ErrorInfo(com.haulmont.restapi.exception.ErrorInfo) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 49 with ResponseEntity

use of org.springframework.http.ResponseEntity in project cuba by cuba-platform.

the class RestControllerExceptionHandler method handleValidationException.

@ExceptionHandler(ValidationException.class)
@ResponseBody
public ResponseEntity<ErrorInfo> handleValidationException(ValidationException e) {
    log.error("ValidationException in service", e);
    ErrorInfo errorInfo = new ErrorInfo("Server error", "");
    return new ResponseEntity<>(errorInfo, HttpStatus.INTERNAL_SERVER_ERROR);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) ErrorInfo(com.haulmont.restapi.exception.ErrorInfo) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 50 with ResponseEntity

use of org.springframework.http.ResponseEntity in project gocd by gocd.

the class AgentRegistrationController method agentRequest.

@RequestMapping(value = "/admin/agent", method = RequestMethod.POST)
public ResponseEntity agentRequest(@RequestParam("hostname") String hostname, @RequestParam("uuid") String uuid, @RequestParam("location") String location, @RequestParam("usablespace") String usablespaceAsString, @RequestParam("operatingSystem") String operatingSystem, @RequestParam("agentAutoRegisterKey") String agentAutoRegisterKey, @RequestParam("agentAutoRegisterResources") String agentAutoRegisterResources, @RequestParam("agentAutoRegisterEnvironments") String agentAutoRegisterEnvironments, @RequestParam("agentAutoRegisterHostname") String agentAutoRegisterHostname, @RequestParam("elasticAgentId") String elasticAgentId, @RequestParam("elasticPluginId") String elasticPluginId, @RequestParam(value = "supportsBuildCommandProtocol", required = false, defaultValue = "false") boolean supportsBuildCommandProtocol, @RequestParam("token") String token, HttpServletRequest request) throws IOException {
    final String ipAddress = request.getRemoteAddr();
    LOG.debug("Processing registration request from agent [{}/{}]", hostname, ipAddress);
    Registration keyEntry;
    String preferredHostname = hostname;
    try {
        if (!Base64.encodeBase64String(hmac().doFinal(uuid.getBytes())).equals(token)) {
            return new ResponseEntity<>("Not a valid token.", FORBIDDEN);
        }
        if (goConfigService.serverConfig().shouldAutoRegisterAgentWith(agentAutoRegisterKey)) {
            preferredHostname = getPreferredHostname(agentAutoRegisterHostname, hostname);
            LOG.info("[Agent Auto Registration] Auto registering agent with uuid {} ", uuid);
        } else {
            if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
                return new ResponseEntity<>(String.format("Elastic agent registration requires an auto-register agent key to be setup on the server. Agent-id: [%s], Plugin-id: [%s]", elasticAgentId, elasticPluginId), UNPROCESSABLE_ENTITY);
            }
        }
        AgentConfig agentConfig = new AgentConfig(uuid, preferredHostname, ipAddress);
        if (partialElasticAgentAutoregistrationInfo(elasticAgentId, elasticPluginId)) {
            return new ResponseEntity<>("Elastic agents must submit both elasticAgentId and elasticPluginId.", UNPROCESSABLE_ENTITY);
        }
        if (elasticAgentIdAlreadyRegistered(elasticAgentId, elasticPluginId)) {
            return new ResponseEntity<>("Duplicate Elastic agent Id used to register elastic agent.", UNPROCESSABLE_ENTITY);
        }
        if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
            agentConfig.setElasticAgentId(elasticAgentId);
            agentConfig.setElasticPluginId(elasticPluginId);
        }
        if (goConfigService.serverConfig().shouldAutoRegisterAgentWith(agentAutoRegisterKey)) {
            LOG.info("[Agent Auto Registration] Auto registering agent with uuid {} ", uuid);
            GoConfigDao.CompositeConfigCommand compositeConfigCommand = new GoConfigDao.CompositeConfigCommand(new AgentConfigService.AddAgentCommand(agentConfig), new UpdateResourceCommand(uuid, agentAutoRegisterResources), new UpdateEnvironmentsCommand(uuid, agentAutoRegisterEnvironments));
            HttpOperationResult result = new HttpOperationResult();
            agentConfig = agentConfigService.updateAgent(compositeConfigCommand, uuid, result, agentService.agentUsername(uuid, ipAddress, preferredHostname));
            if (!result.isSuccess()) {
                List<ConfigErrors> errors = ErrorCollector.getAllErrors(agentConfig);
                ConfigErrors e = new ConfigErrors();
                e.add("resultMessage", result.detailedMessage());
                errors.add(e);
                throw new GoConfigInvalidException(null, new AllConfigErrors(errors).asString());
            }
        }
        boolean registeredAlready = goConfigService.hasAgent(uuid);
        long usableSpace = Long.parseLong(usablespaceAsString);
        AgentRuntimeInfo agentRuntimeInfo = AgentRuntimeInfo.fromServer(agentConfig, registeredAlready, location, usableSpace, operatingSystem, supportsBuildCommandProtocol);
        if (elasticAgentAutoregistrationInfoPresent(elasticAgentId, elasticPluginId)) {
            agentRuntimeInfo = ElasticAgentRuntimeInfo.fromServer(agentRuntimeInfo, elasticAgentId, elasticPluginId);
        }
        keyEntry = agentService.requestRegistration(agentService.agentUsername(uuid, ipAddress, preferredHostname), agentRuntimeInfo);
        final HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return new ResponseEntity<>(RegistrationJSONizer.toJson(keyEntry), httpHeaders, keyEntry.isValid() ? OK : ACCEPTED);
    } catch (Exception e) {
        LOG.error("Error occurred during agent registration process: ", e);
        return new ResponseEntity<>(String.format("Error occurred during agent registration process: %s", getErrorMessage(e)), UNPROCESSABLE_ENTITY);
    }
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) HttpHeaders(org.springframework.http.HttpHeaders) UpdateEnvironmentsCommand(com.thoughtworks.go.config.update.UpdateEnvironmentsCommand) GoConfigDao(com.thoughtworks.go.config.GoConfigDao) GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) AllConfigErrors(com.thoughtworks.go.domain.AllConfigErrors) GoConfigInvalidException(com.thoughtworks.go.config.exceptions.GoConfigInvalidException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AgentConfig(com.thoughtworks.go.config.AgentConfig) ResponseEntity(org.springframework.http.ResponseEntity) Registration(com.thoughtworks.go.security.Registration) UpdateResourceCommand(com.thoughtworks.go.config.update.UpdateResourceCommand) AllConfigErrors(com.thoughtworks.go.domain.AllConfigErrors) ConfigErrors(com.thoughtworks.go.domain.ConfigErrors) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)1188 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)419 HttpHeaders (org.springframework.http.HttpHeaders)398 Test (org.junit.Test)120 ApiOperation (io.swagger.annotations.ApiOperation)116 RestAccessControl (org.entando.entando.web.common.annotation.RestAccessControl)108 HashMap (java.util.HashMap)104 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)103 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)98 HttpStatus (org.springframework.http.HttpStatus)88 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)85 ArrayList (java.util.ArrayList)80 GetMapping (org.springframework.web.bind.annotation.GetMapping)79 Timed (com.codahale.metrics.annotation.Timed)68 IOException (java.io.IOException)67 List (java.util.List)65 URI (java.net.URI)49 MediaType (org.springframework.http.MediaType)48 Test (org.junit.jupiter.api.Test)46 HttpEntity (org.springframework.http.HttpEntity)46