Java tutorial
/** * Copyright (C) 2015 Matt Christiansen (matt@nikore.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.nikore.gozer.marathon; import java.io.IOException; import java.util.Map; import java.util.concurrent.ExecutionException; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.inject.Inject; import com.google.inject.Singleton; @Singleton public class CallbackServlet extends HttpServlet { private final ObjectMapper mapper; private final MarathonAppCache appCache; @Inject public CallbackServlet(ObjectMapper mapper, MarathonAppCache appCache) { this.mapper = mapper; this.appCache = appCache; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { Map<String, Object> eventResponse = mapper.readValue(servletRequest.getInputStream(), new TypeReference<Map<String, Object>>() { }); switch ((String) eventResponse.get("eventType")) { case "status_update_event": String taskStatus = (String) eventResponse.get("taskStatus"); if (!taskStatus.equals("TASK_STAGING") || !taskStatus.equals("TASK_STARTING")) { appCache.refreshClient((String) eventResponse.get("appId")); } break; case "api_post_event": Map<String, String> appDef = (Map<String, String>) eventResponse.get("appDefinition"); try { appCache.removeClient(appDef.get("id")); appCache.getClient(appDef.get("id")); } catch (ExecutionException e) { throw new ServletException(e); } break; case "app_terminated_event": appCache.removeClient((String) eventResponse.get("appId")); break; default: break; // } } }