|
| 1 | +package com.devicehive.proxy; |
| 2 | + |
| 3 | +/* |
| 4 | + * #%L |
| 5 | + * DeviceHive Backend Logic |
| 6 | + * %% |
| 7 | + * Copyright (C) 2016 - 2017 DataArt |
| 8 | + * %% |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * #L% |
| 21 | + */ |
| 22 | + |
| 23 | +import com.devicehive.application.RequestHandlersMapper; |
| 24 | +import com.devicehive.proxy.api.NotificationHandler; |
| 25 | +import com.devicehive.proxy.api.ProxyClient; |
| 26 | +import com.devicehive.proxy.api.ProxyMessage; |
| 27 | +import com.devicehive.proxy.api.ProxyMessageBuilder; |
| 28 | +import com.devicehive.proxy.api.payload.NotificationCreatePayload; |
| 29 | +import com.devicehive.shim.api.Action; |
| 30 | +import com.devicehive.shim.api.Request; |
| 31 | +import com.devicehive.shim.api.Response; |
| 32 | +import com.devicehive.shim.api.server.RequestHandler; |
| 33 | +import com.google.gson.Gson; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | +import org.springframework.beans.factory.annotation.Autowired; |
| 37 | +import org.springframework.stereotype.Component; |
| 38 | + |
| 39 | +import java.util.Optional; |
| 40 | + |
| 41 | +@Component |
| 42 | +public class ProxyRequestHandler implements NotificationHandler { |
| 43 | + |
| 44 | + private static final Logger logger = LoggerFactory.getLogger(ProxyRequestHandler.class); |
| 45 | + |
| 46 | + private final Gson gson; |
| 47 | + private final RequestHandlersMapper requestHandlersMapper; |
| 48 | + |
| 49 | + @Autowired |
| 50 | + public ProxyRequestHandler(Gson gson, RequestHandlersMapper requestHandlersMapper) { |
| 51 | + this.gson = gson; |
| 52 | + this.requestHandlersMapper = requestHandlersMapper; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void handle(String message, ProxyClient client) { |
| 57 | + logger.debug("Received message from proxy client: " + message); |
| 58 | + final Request request = gson.fromJson(message, Request.class); |
| 59 | + final String replyTo = request.getReplyTo(); |
| 60 | + |
| 61 | + Response response; |
| 62 | + |
| 63 | + switch (request.getType()) { |
| 64 | + case clientRequest: |
| 65 | + logger.debug("Client request received {}", request); |
| 66 | + response = handleClientRequest(request); |
| 67 | + break; |
| 68 | + case ping: |
| 69 | + logger.info("Ping request received from {}", replyTo); |
| 70 | + response = Response.newBuilder().buildSuccess(); |
| 71 | + break; |
| 72 | + default: |
| 73 | + logger.warn("Unknown type of request received {} from client with topic {}, correlationId = {}", |
| 74 | + request.getType(), replyTo, request.getCorrelationId()); |
| 75 | + response = Response.newBuilder() |
| 76 | + .buildFailed(404); |
| 77 | + } |
| 78 | + |
| 79 | + // set correlationId explicitly to prevent missing it in request |
| 80 | + response.setCorrelationId(request.getCorrelationId()); |
| 81 | + ProxyMessage responseMessage = ProxyMessageBuilder.notification(new NotificationCreatePayload(replyTo, gson.toJson(response))); |
| 82 | + client.push(responseMessage); |
| 83 | + } |
| 84 | + |
| 85 | + private Response handleClientRequest(Request request) { |
| 86 | + Response response; |
| 87 | + final Action action = request.getBody().getAction(); |
| 88 | + |
| 89 | + RequestHandler requestHandler = requestHandlersMapper.requestHandlerMap().get(action); |
| 90 | + if (requestHandler == null) { |
| 91 | + throw new RuntimeException("Action '" + action + "' is not supported."); |
| 92 | + } |
| 93 | + try { |
| 94 | + response = Optional.ofNullable(requestHandler.handle(request)) |
| 95 | + .orElseThrow(() -> new NullPointerException("Response must not be null")); |
| 96 | + } catch (Exception e) { |
| 97 | + logger.error("Unexpected exception occurred during request handling (action='{}', handler='{}')", |
| 98 | + request.getBody().getAction().name(), requestHandler.getClass().getCanonicalName(), e); |
| 99 | + |
| 100 | + response = Response.newBuilder() |
| 101 | + .withLast(request.isSingleReplyExpected()) |
| 102 | + .buildFailed(500); |
| 103 | + } |
| 104 | + return response; |
| 105 | + } |
| 106 | +} |
0 commit comments