/hazelcast/src/main/java/com/hazelcast/impl/ascii/rest/HttpDeleteCommandProcessor.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 67 lines · 44 code · 6 blank · 17 comment · 14 complexity · 61d21b1f040148e16002450c7e2817d5 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl.ascii.rest;
  17. import com.hazelcast.impl.ThreadContext;
  18. import com.hazelcast.impl.ascii.TextCommandService;
  19. public class HttpDeleteCommandProcessor extends HttpCommandProcessor<HttpDeleteCommand> {
  20. public HttpDeleteCommandProcessor(TextCommandService textCommandService) {
  21. super(textCommandService);
  22. }
  23. public void handle(HttpDeleteCommand command) {
  24. String uri = command.getURI();
  25. if (uri.startsWith(URI_MAPS)) {
  26. int indexEnd = uri.indexOf('/', URI_MAPS.length());
  27. String mapName = uri.substring(URI_MAPS.length(), indexEnd);
  28. String key = uri.substring(indexEnd + 1);
  29. Object value = textCommandService.delete(mapName, key);
  30. command.send204();
  31. } else if (uri.startsWith(URI_QUEUES)) {
  32. // Poll an item from the default queue in 3 seconds
  33. // http://127.0.0.1:5701/hazelcast/rest/queues/default/3
  34. int indexEnd = uri.indexOf('/', URI_QUEUES.length());
  35. String queueName = uri.substring(URI_QUEUES.length(), indexEnd);
  36. String secondStr = (uri.length() > (indexEnd + 1)) ? uri.substring(indexEnd + 1) : null;
  37. int seconds = (secondStr == null) ? 0 : Integer.parseInt(secondStr);
  38. Object value = textCommandService.poll(queueName, seconds);
  39. if (value == null) {
  40. command.send204();
  41. } else {
  42. if (value instanceof byte[]) {
  43. command.setResponse(null, (byte[]) value);
  44. } else if (value instanceof RestValue) {
  45. RestValue restValue = (RestValue) value;
  46. command.setResponse(restValue.getContentType(), restValue.getValue());
  47. } else if (value instanceof String) {
  48. command.setResponse(HttpCommand.CONTENT_TYPE_PLAIN_TEXT, ((String) value).getBytes());
  49. } else {
  50. command.setResponse(null, ThreadContext.get().toByteArray(value));
  51. }
  52. }
  53. } else {
  54. command.send400();
  55. }
  56. textCommandService.sendResponse(command);
  57. }
  58. public void handleRejection(HttpDeleteCommand command) {
  59. handle(command);
  60. }
  61. }