List of usage examples for org.springframework.http HttpMethod DELETE
HttpMethod DELETE
To view the source code for org.springframework.http HttpMethod DELETE.
Click Source Link
From source file:org.venice.beachfront.bfapi.services.PiazzaService.java
/** * Requests deletion of a single Data item from Piazza. * /*from w ww. j av a 2 s. co m*/ * @param dataId * The ID of the Data to delete */ private void deleteData(String dataId) throws UserException { try { // Form request String deleteUrl = String.format("%s/data/%s", PIAZZA_URL, dataId); piazzaLogger.log(String.format("Requesting data %s be deleted from Piazza at %s", dataId, deleteUrl), Severity.INFORMATIONAL); HttpHeaders headers = createPiazzaHeaders(PIAZZA_API_KEY); HttpEntity<String> request = new HttpEntity<>(headers); // Request the Deletion restTemplate.exchange(URI.create(deleteUrl), HttpMethod.DELETE, request, String.class); // Log success piazzaLogger.log(String.format("Data %s successfully deleted from Piazza", dataId), Severity.INFORMATIONAL); } catch (HttpClientErrorException | HttpServerErrorException exception) { piazzaLogger.log(String.format("Error Deleting Data %s from Piazza. Failed with Code %s and Body %s", dataId, exception.getStatusText(), exception.getResponseBodyAsString()), Severity.ERROR); throw new UserException("Upstream error deleting data", exception, exception.getStatusCode()); } catch (Exception exception) { exception.printStackTrace(); piazzaLogger.log(String.format("Unexpected Error Deleting Data %s from Piazza. %s", dataId, exception.getMessage()), Severity.ERROR); throw new UserException("Unexpected error deleting data", exception, HttpStatus.INTERNAL_SERVER_ERROR); } }
From source file:philharmonic.utilities.MessageSender.java
public ResponseEntity<String> sendMessage(Message message, int id, String body) { String target = message.getTargetComponentName(); String URI;//from ww w.j a va2 s. com URI = parser.getAddressForComponent(target) + "/" + target + "/" + message.getResourceName() + "/" + id; HttpEntity entity = new HttpEntity(body, createHeaders(target)); if (message.getAction().equals(nameDELETEAction)) { return rt.exchange(URI, HttpMethod.DELETE, entity, String.class); } if (message.getAction().equals(nameGETAction)) { return rt.exchange(URI, HttpMethod.GET, entity, String.class); } return null; }
From source file:sg.ncl.DataController.java
@RequestMapping(value = { "/remove/{id}", "/remove/{id}/{admin}" }) public String removeDataset(@PathVariable String id, @PathVariable Optional<String> admin, RedirectAttributes redirectAttributes) throws WebServiceRuntimeException { HttpEntity<String> request = createHttpEntityHeaderOnly(); restTemplate.setErrorHandler(new MyResponseErrorHandler()); ResponseEntity response = restTemplate.exchange(properties.getDataset(id), HttpMethod.DELETE, request, String.class); String dataResponseBody = response.getBody().toString(); try {// ww w . j a va 2s .c om if (RestUtil.isError(response.getStatusCode())) { MyErrorResource error = objectMapper.readValue(dataResponseBody, MyErrorResource.class); ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError()); if (exceptionState == FORBIDDEN_EXCEPTION) { log.warn("Removing of dataset forbidden."); redirectAttributes.addFlashAttribute(MESSAGE_ATTRIBUTE, error.getMessage()); } else if (exceptionState == DATA_NOT_FOUND_EXCEPTION) { log.warn("Dataset not found for removing."); redirectAttributes.addFlashAttribute(MESSAGE_ATTRIBUTE, error.getMessage()); } else { log.warn("Unknown error for removing dataset."); } } else { log.info("Dataset removed: {}", dataResponseBody); } } catch (IOException e) { log.error("removeDataset: {}", e.toString()); throw new WebServiceRuntimeException(e.getMessage()); } if (admin.isPresent()) { return "redirect:/admin/data"; } return REDIRECT_DATA; }
From source file:sg.ncl.DataController.java
@RequestMapping(value = "{datasetId}/resources/{resourceId}/delete", method = RequestMethod.GET) public String removeResource(@PathVariable String datasetId, @PathVariable String resourceId, RedirectAttributes redirectAttributes) throws WebServiceRuntimeException { HttpEntity<String> request = createHttpEntityHeaderOnly(); restTemplate.setErrorHandler(new MyResponseErrorHandler()); ResponseEntity response = restTemplate.exchange(properties.getResource(datasetId, resourceId), HttpMethod.DELETE, request, String.class); String dataResponseBody = response.getBody().toString(); try {/* w w w. j av a 2 s . c o m*/ if (RestUtil.isError(response.getStatusCode())) { MyErrorResource error = objectMapper.readValue(dataResponseBody, MyErrorResource.class); ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError()); if (exceptionState == FORBIDDEN_EXCEPTION) { log.warn("Removing of data resource forbidden."); redirectAttributes.addFlashAttribute(MESSAGE_ATTRIBUTE, error.getMessage()); } else if (exceptionState == DATA_NOT_FOUND_EXCEPTION) { log.warn("Dataset not found for removing resource."); redirectAttributes.addFlashAttribute(MESSAGE_ATTRIBUTE, error.getMessage()); } else if (exceptionState == DATA_RESOURCE_NOT_FOUND_EXCEPTION) { log.warn("Data resource not found for removing."); redirectAttributes.addFlashAttribute(MESSAGE_ATTRIBUTE, error.getMessage()); } else if (exceptionState == DATA_RESOURCE_DELETE_EXCEPTION) { log.warn("Error when removing data resource file."); redirectAttributes.addFlashAttribute(MESSAGE_ATTRIBUTE, error.getMessage()); } else { log.warn("Unknown error for removing data resource."); } } else { log.info("Data resource removed: {}", dataResponseBody); } } catch (IOException e) { log.error("removeResource: {}", e); throw new WebServiceRuntimeException(e.getMessage()); } return "redirect:/data/" + datasetId + "/" + RESOURCES; }
From source file:sg.ncl.MainController.java
@RequestMapping("/approve_new_user/reject/{teamId}/{userId}") public String userSideRejectJoinRequest(@PathVariable String teamId, @PathVariable String userId, HttpSession session, RedirectAttributes redirectAttributes) throws WebServiceRuntimeException { log.info("Reject join request: User {}, Team {}, Approver {}", userId, teamId, session.getAttribute("id").toString()); JSONObject mainObject = new JSONObject(); JSONObject userFields = new JSONObject(); userFields.put("id", session.getAttribute("id").toString()); mainObject.put("user", userFields); HttpEntity<String> request = createHttpEntityWithBody(mainObject.toString()); ResponseEntity response;// w ww . ja v a 2 s . co m try { response = restTemplate.exchange(properties.getRejectJoinRequest(teamId, userId), HttpMethod.DELETE, request, String.class); } catch (RestClientException e) { log.warn("Error connecting to sio team service: {}", e); redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD); return REDIRECT_APPROVE_NEW_USER; } String responseBody = response.getBody().toString(); if (RestUtil.isError(response.getStatusCode())) { try { MyErrorResource error = objectMapper.readValue(responseBody, MyErrorResource.class); ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError()); switch (exceptionState) { case DETERLAB_OPERATION_FAILED_EXCEPTION: log.warn("Reject join request: User {}, Team {} fail", userId, teamId); redirectAttributes.addFlashAttribute(MESSAGE, "Reject join request fail"); break; default: log.warn("Server side error: {}", error.getError()); redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD); break; } return REDIRECT_APPROVE_NEW_USER; } catch (IOException ioe) { log.warn(LOG_IOEXCEPTION, ioe); throw new WebServiceRuntimeException(ioe.getMessage()); } } // everything looks OK? log.info("Join request has been REJECTED, User {}, Team {}", userId, teamId); redirectAttributes.addFlashAttribute(MESSAGE, "Join request has been REJECTED."); return REDIRECT_APPROVE_NEW_USER; }
From source file:sg.ncl.MainController.java
@RequestMapping("/teams/delete_image/{teamId}/{imageName}") public String deleteImage(@PathVariable String teamId, @PathVariable String imageName, final RedirectAttributes redirectAttributes) throws WebServiceRuntimeException { String errorMessage = "Error in deleting image {} from team '{}' "; String imageMessage = "The image " + "\'" + imageName + "\'"; log.info("Deleting image {} from team {}", imageName, teamId); try {//ww w . jav a 2 s.c o m HttpEntity<String> request = createHttpEntityHeaderOnly(); restTemplate.setErrorHandler(new MyResponseErrorHandler()); ResponseEntity response = restTemplate.exchange(properties.deleteImage(teamId, imageName), HttpMethod.DELETE, request, String.class); String responseBody = response.getBody().toString(); if (RestUtil.isError(response.getStatusCode())) { MyErrorResource error = objectMapper.readValue(responseBody, MyErrorResource.class); ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError()); switch (exceptionState) { case INSUFFICIENT_PERMISSION_EXCEPTION: log.warn(errorMessage + ": insufficient permission", imageName, teamId); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, "You do not have permission to delete this image. Only " + " team leader or creator of this image can delete the image."); break; case TEAM_NOT_FOUND_EXCEPTION: log.warn(errorMessage + ": team not found", imageName, teamId); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, "Team '" + teamId + "' is not found"); break; case IMAGE_NOT_FOUND_EXCEPTION: log.warn(errorMessage + ": image does not exist or not found in teams' list of images", imageName, teamId); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, imageMessage + " either does not exist or not found in your teams' list of images"); break; case DETERLAB_OPERATION_FAILED_EXCEPTION: log.warn(errorMessage + ": operation failed on DeterLab", imageName, teamId); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, ERR_SERVER_OVERLOAD); break; case ADAPTER_CONNECTION_EXCEPTION: log.warn(errorMessage + ": adapter connection error", imageName, teamId); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, ERR_SERVER_OVERLOAD); break; case ADAPTER_INTERNAL_ERROR_EXCEPTION: log.warn(errorMessage + ": adapter internal error", imageName, teamId); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, ERR_SERVER_OVERLOAD); break; default: log.warn(errorMessage + ": {}", imageName, teamId, exceptionState); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, ERR_SERVER_OVERLOAD); break; } return REDIRECT_TEAMS; } else { String sioMessage = new JSONObject(responseBody).getString("msg"); switch (sioMessage) { case "image still in use": log.warn(errorMessage + ": {}", imageName, teamId, sioMessage); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, imageMessage + " is still in use or busy!"); // show experiments list // string experiments is passed from adapter // truncate the square brackets in front and behind if (responseBody.contains(EXPERIMENTS)) { String experiments = new JSONObject(responseBody).getJSONArray(EXPERIMENTS).toString(); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE_LIST, experiments.substring(1, experiments.length() - 1)); } break; // curl command is ok but there is problem with rm command case "delete image OK from web but there is unknown error when deleting physical image": log.warn(errorMessage + ": {}", imageName, teamId, sioMessage); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_WARNING, imageMessage + " is successfully deleted. " + "However, " + ERR_SERVER_OVERLOAD); break; default: log.info("Deleting image '{}' of team '{}' is successful ", imageName, teamId); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_SUCCESS, imageMessage + " is successfully deleted"); break; } return REDIRECT_TEAMS; } } catch (IOException e) { log.warn("Error connecting to sio image service for deleting image: {}", e.getMessage()); redirectAttributes.addFlashAttribute(MESSAGE_DELETE_IMAGE_FAILURE, ERR_SERVER_OVERLOAD); throw new WebServiceRuntimeException(e.getMessage()); } }
From source file:sg.ncl.MainController.java
@RequestMapping("/remove_member/{teamId}/{userId}") public String removeMember(@PathVariable String teamId, @PathVariable String userId, final RedirectAttributes redirectAttributes) throws IOException { JSONObject teamMemberFields = new JSONObject(); teamMemberFields.put(USER_ID, userId); teamMemberFields.put(MEMBER_TYPE, MemberType.MEMBER.name()); teamMemberFields.put(MEMBER_STATUS, MemberStatus.APPROVED.name()); HttpEntity<String> request = createHttpEntityWithBody(teamMemberFields.toString()); restTemplate.setErrorHandler(new MyResponseErrorHandler()); ResponseEntity response;/*from w w w . j ava 2s . c o m*/ try { response = restTemplate.exchange(properties.removeUserFromTeam(teamId), HttpMethod.DELETE, request, String.class); } catch (RestClientException e) { log.warn("Error connecting to sio team service for remove user: {}", e); redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD); return REDIRECT_TEAM_PROFILE_TEAM_ID; } String responseBody = response.getBody().toString(); User2 user = invokeAndExtractUserInfo(userId); String name = user.getFirstName() + TAG_SPACE + user.getLastName(); if (RestUtil.isError(response.getStatusCode())) { MyErrorResource error = objectMapper.readValue(responseBody, MyErrorResource.class); ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError()); switch (exceptionState) { case DETERLAB_OPERATION_FAILED_EXCEPTION: // two subcases when fail to remove users from team log.warn("Remove member from team: User {}, Team {} fail - {}", userId, teamId, error.getMessage()); if ("user has experiments".equals(error.getMessage())) { /* case 1 - user has experiments display the list of experiments that have to be terminated first since the team profile page has experiments already, we don't have to retrieve them again use the userid to filter out the experiment list at the web pages */ redirectAttributes.addFlashAttribute(MESSAGE, ERROR_PREFIX + " Member " + name + " has experiments."); redirectAttributes.addFlashAttribute(REMOVE_MEMBER_UID, userId); redirectAttributes.addFlashAttribute(REMOVE_MEMBER_NAME, name); break; } else { // case 2 - deterlab operation failure log.warn("Remove member from team: deterlab operation failed"); redirectAttributes.addFlashAttribute(MESSAGE, ERROR_PREFIX + " Member " + name + " cannot be removed."); break; } default: log.warn("Server side error for remove members: {}", error.getError()); redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD); break; } } else { log.info("Remove member: {}", response.getBody().toString()); // add success message redirectAttributes.addFlashAttribute(MESSAGE_SUCCESS, "Member " + name + " has been removed."); } return REDIRECT_TEAM_PROFILE_TEAM_ID; }
From source file:sg.ncl.MainController.java
@RequestMapping("/remove_experiment/{teamName}/{teamId}/{expId}") public String removeExperiment(@PathVariable String teamName, @PathVariable String teamId, @PathVariable String expId, final RedirectAttributes redirectAttributes, HttpSession session) throws WebServiceRuntimeException { // ensure experiment is stopped first Realization realization = invokeAndExtractRealization(teamName, Long.parseLong(expId)); Team2 team = invokeAndExtractTeamInfo(teamId); // check valid authentication to remove experiments // either admin, experiment creator or experiment owner if (!validateIfAdmin(session) && !realization.getUserId().equals(session.getAttribute("id").toString()) && !team.getOwner().getId().equals(session.getAttribute(webProperties.getSessionUserId()))) { log.warn("Permission denied when remove Team:{}, Experiment: {} with User: {}, Role:{}", teamId, expId, session.getAttribute("id"), session.getAttribute(webProperties.getSessionRoles())); redirectAttributes.addFlashAttribute(MESSAGE, "An error occurred while trying to remove experiment;" + permissionDeniedMessage); return REDIRECT_EXPERIMENTS; }//ww w.ja va2s.c om if (!realization.getState().equals(RealizationState.NOT_RUNNING.toString())) { log.warn("Trying to remove Team: {}, Experiment: {} with State: {} that is still in progress?", teamId, expId, realization.getState()); redirectAttributes.addFlashAttribute(MESSAGE, "An error occurred while trying to remove Exp: " + realization.getExperimentName() + REFRESH + CONTACT_EMAIL); return REDIRECT_EXPERIMENTS; } log.info("Removing experiment: at " + properties.getDeleteExperiment(teamId, expId)); HttpEntity<String> request = createHttpEntityHeaderOnly(); restTemplate.setErrorHandler(new MyResponseErrorHandler()); ResponseEntity response; try { response = restTemplate.exchange(properties.getDeleteExperiment(teamId, expId), HttpMethod.DELETE, request, String.class); } catch (Exception e) { log.warn("Error connecting to experiment service to remove experiment", e.getMessage()); redirectAttributes.addFlashAttribute(MESSAGE, ERR_SERVER_OVERLOAD); return REDIRECT_EXPERIMENTS; } String responseBody = response.getBody().toString(); try { if (RestUtil.isError(response.getStatusCode())) { MyErrorResource error = objectMapper.readValue(responseBody, MyErrorResource.class); ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError()); switch (exceptionState) { case EXPERIMENT_DELETE_EXCEPTION: case FORBIDDEN_EXCEPTION: log.warn("remove experiment failed for Team: {}, Exp: {}", teamId, expId); redirectAttributes.addFlashAttribute(MESSAGE, error.getMessage()); break; case OBJECT_OPTIMISTIC_LOCKING_FAILURE_EXCEPTION: // do nothing log.info("remove experiment database locking failure"); break; default: // do nothing break; } return REDIRECT_EXPERIMENTS; } else { // everything ok log.info("remove experiment success for Team: {}, Exp: {}", teamId, expId); redirectAttributes.addFlashAttribute("exp_remove_message", "Team: " + teamName + " has removed Exp: " + realization.getExperimentName()); return REDIRECT_EXPERIMENTS; } } catch (IOException e) { throw new WebServiceRuntimeException(e.getMessage()); } }
From source file:sg.ncl.MainController.java
@RequestMapping(value = "/admin/gpus/{gpu}/remove/{userid}", method = RequestMethod.GET) public String adminRemoveGpuUser(@PathVariable("gpu") Integer gpu, @PathVariable("userid") String userid, RedirectAttributes redirectAttributes, HttpSession session) throws WebServiceRuntimeException { if (!validateIfAdmin(session)) { return NO_PERMISSION_PAGE; }/*w w w .j av a 2s . c o m*/ GpuProperties.Domain domain = gpuProperties.getDomains().get(gpu); String url = "http://" + domain.getHost() + ":" + domain.getPort() + "/users/" + userid; HttpEntity<String> request = createHttpEntityHeaderOnlyNoAuthHeader(); ResponseEntity response = restTemplate.exchange(url, HttpMethod.DELETE, request, String.class); String responseBody = response.getBody().toString(); JSONObject jsonObject = new JSONObject(responseBody); String message = jsonObject.getString(userid); if (message.contains("Failed")) { redirectAttributes.addFlashAttribute("message", message + " '" + userid + "'"); } else { redirectAttributes.addFlashAttribute("messageSuccess", message + " '" + userid + "'"); } redirectAttributes.addFlashAttribute("gpuUsersMap", getGpuUsers(gpu)); redirectAttributes.addFlashAttribute("selectedGpu", gpu); return "redirect:/admin/gpus"; }
From source file:sg.ncl.MainController.java
@GetMapping("/admin/monthly/remove/{id}") public String adminMonthlyRemove(@PathVariable String id, RedirectAttributes attr, HttpSession session) throws WebServiceRuntimeException { if (!validateIfAdmin(session)) { return NO_PERMISSION_PAGE; }//from ww w.j av a 2 s. com HttpEntity<String> request = createHttpEntityHeaderOnly(); restTemplate.setErrorHandler(new MyResponseErrorHandler()); ResponseEntity response = restTemplate.exchange(properties.getMonthly() + "/" + id, HttpMethod.DELETE, request, String.class); String responseBody = response.getBody().toString(); try { if (RestUtil.isError(response.getStatusCode())) { MyErrorResource error = objectMapper.readValue(responseBody, MyErrorResource.class); ExceptionState exceptionState = ExceptionState.parseExceptionState(error.getError()); switch (exceptionState) { case PROJECT_DETAILS_NOT_FOUND_EXCEPTION: log.warn("Project not found for deleting"); attr.addFlashAttribute(MESSAGE, "Error(s):<ul><li>project not found for deleting</li></ul>"); break; case FORBIDDEN_EXCEPTION: log.warn("Removing of project forbidden."); attr.addFlashAttribute(MESSAGE, "Error(s):<ul><li>deleting project forbidden</li></ul>"); break; default: log.warn("Unknown error for validating project."); attr.addFlashAttribute(MESSAGE, "Error(s):<ul><li>unknown error for deleting project</li></ul>"); } } else { log.info("Project details deleted: {}", responseBody); } } catch (IOException e) { log.error("adminMonthlyRemove: {}", e.toString()); throw new WebServiceRuntimeException(e.getMessage()); } return "redirect:/admin/monthly"; }