Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phuongtailtranminh/b7a8be59680c13823943643ec3c56351 to your computer and use it in GitHub Desktop.
Save phuongtailtranminh/b7a8be59680c13823943643ec3c56351 to your computer and use it in GitHub Desktop.
Echo Controller
package com.viettel.integration.hub.echoserver.controller;
import java.io.IOException;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class HomeController {
private static final Logger LOG = LoggerFactory.getLogger(HomeController.class);
@Autowired
private HttpServletRequest request;
@RequestMapping(value = "/**", consumes = MediaType.ALL_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS})
public ResponseEntity<Map<String, Object>> echoBack(@RequestBody(required = false) byte[] rawBody) throws IOException {
Map<String, String> headers = new HashMap<>();
for (String headerName : Collections.list(request.getHeaderNames())) {
headers.put(headerName, request.getHeader(headerName));
}
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("protocol", request.getProtocol());
responseMap.put("method", request.getMethod());
responseMap.put("headers", headers);
responseMap.put("cookies", request.getCookies());
responseMap.put("parameters", request.getParameterMap());
responseMap.put("path", request.getServletPath());
responseMap.put("body", rawBody != null ? Base64.getEncoder().encodeToString(rawBody) : null);
LOG.info("REQUEST: " + request.getParameterMap());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(responseMap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment