PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/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
17package com.hazelcast.impl.ascii.rest;
18
19import com.hazelcast.impl.ThreadContext;
20import com.hazelcast.impl.ascii.TextCommandService;
21
22public class HttpDeleteCommandProcessor extends HttpCommandProcessor<HttpDeleteCommand> {
23
24    public HttpDeleteCommandProcessor(TextCommandService textCommandService) {
25        super(textCommandService);
26    }
27
28    public void handle(HttpDeleteCommand command) {
29        String uri = command.getURI();
30        if (uri.startsWith(URI_MAPS)) {
31            int indexEnd = uri.indexOf('/', URI_MAPS.length());
32            String mapName = uri.substring(URI_MAPS.length(), indexEnd);
33            String key = uri.substring(indexEnd + 1);
34            Object value = textCommandService.delete(mapName, key);
35            command.send204();
36        } else if (uri.startsWith(URI_QUEUES)) {
37            // Poll an item from the default queue in 3 seconds
38            // http://127.0.0.1:5701/hazelcast/rest/queues/default/3
39            int indexEnd = uri.indexOf('/', URI_QUEUES.length());
40            String queueName = uri.substring(URI_QUEUES.length(), indexEnd);
41            String secondStr = (uri.length() > (indexEnd + 1)) ? uri.substring(indexEnd + 1) : null;
42            int seconds = (secondStr == null) ? 0 : Integer.parseInt(secondStr);
43            Object value = textCommandService.poll(queueName, seconds);
44            if (value == null) {
45                command.send204();
46            } else {
47                if (value instanceof byte[]) {
48                    command.setResponse(null, (byte[]) value);
49                } else if (value instanceof RestValue) {
50                    RestValue restValue = (RestValue) value;
51                    command.setResponse(restValue.getContentType(), restValue.getValue());
52                } else if (value instanceof String) {
53                    command.setResponse(HttpCommand.CONTENT_TYPE_PLAIN_TEXT, ((String) value).getBytes());
54                } else {
55                    command.setResponse(null, ThreadContext.get().toByteArray(value));
56                }
57            }
58        } else {
59            command.send400();
60        }
61        textCommandService.sendResponse(command);
62    }
63
64    public void handleRejection(HttpDeleteCommand command) {
65        handle(command);
66    }
67}