All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.csp.ahas.switchcenter.AhasRequestHandler Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package com.alibaba.csp.ahas.switchcenter;

import com.taobao.csp.ahas.transport.api.AgwRequestHandler;
import com.taobao.csp.ahas.transport.api.Request;
import com.taobao.csp.ahas.transport.api.Response;
import com.taobao.csp.ahas.transport.api.Response.Code;
import com.taobao.csp.switchcenter.log.SwitchRecordLog;

/**
 * @param 
 * @author changjun.xcj
 */
public class AhasRequestHandler extends AgwRequestHandler {

	@SuppressWarnings("rawtypes")
	private com.taobao.csp.switchcenter.command.CommandHandler commandHandler;

	public AhasRequestHandler(com.taobao.csp.switchcenter.command.CommandHandler commandHandler) {
		this.commandHandler = commandHandler;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Response handle(Request request) {
		if (commandHandler == null) {
			return Response.ofFailure(Code.NOT_FOUND, "Cannot find command handler");
		}
		return convertResponse(commandHandler.handle(convertRequest(request)));
	}

	/**
	 * Convert AHAS request to Sentinel request
	 *
	 * @param request
	 * @return
	 */
	private com.taobao.csp.switchcenter.command.CommandRequest convertRequest(Request request) {

		com.taobao.csp.switchcenter.command.CommandRequest commandRequest = new com.taobao.csp.switchcenter.command.CommandRequest();
		if (request == null) {
			return commandRequest;
		}
		commandRequest.getMetadata().putAll(request.getHeaders());
		commandRequest.getParameters().putAll(request.getParams());
		return commandRequest;
	}

	/**
	 * Convert Sentinel response to AHAS response
	 *
	 * @param response
	 * @return
	 */
	private Response convertResponse(com.taobao.csp.switchcenter.command.CommandResponse response) {
		if (response == null) {
			return Response.ofFailure(Code.TIMEOUT, "response is null");
		}
		if (response.isSuccess()) {
			return Response.ofSuccess(response.getResult());
		}
		return Response.ofFailure(Code.SERVER_ERROR, response.getException().getMessage());
	}

	@Override
	public void logHandleException(Request request, Exception exception) {
		SwitchRecordLog.info("logHandleException, " + request, exception);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy