Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add headers, panic, echo and store utilities #728

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,32 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

@Controller
@RequestMapping("/utility")
public class UtilityController {

private final ApplicationContext context;

public UtilityController(ApplicationContext context) {
this.context = context;
}

@GetMapping("/stress/{iterations}")
@ResponseBody
public double stress(@PathVariable int iterations) {
Expand All @@ -51,4 +69,62 @@ private double monteCarloPi(int iterations) {
public ResponseEntity<String> status(@PathVariable int code) {
return ResponseEntity.status(code).body("OK");
}

@GetMapping(value = "/headers", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, List<String>>> statusHeaders(@RequestHeader HttpHeaders headers) {
return ResponseEntity.ok()
.body(headers);
}

@GetMapping("/panic")
public ResponseEntity<String> panic() {
Thread thread = new Thread(() -> {
try {
Thread.sleep(500); // Small delay to allow response to be sent
SpringApplication.exit(context, () -> 1);
System.exit(255);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
thread.start();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Shutting down...");
}

// function /echo that answer the hash provided as POST input
@PostMapping("/echo")
@ResponseBody
public ResponseEntity<String> echo(@RequestBody String body) {
return ResponseEntity.ok()
.body(body);
}

// function /store what take a POST hash to write to a locally created file
@PostMapping("/store")
@ResponseBody
public ResponseEntity<String> store(@RequestBody String body) {
String filename = String.valueOf(Math.abs(body.hashCode())); // ensure positive number
try (java.io.FileWriter fileWriter = new java.io.FileWriter("/tmp/" + filename + ".json")) {
fileWriter.write(body);
return ResponseEntity.ok()
.body("{\"hash\": \"" + filename + "\"}");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Error writing file: " + e.getMessage());
}
}

// function /store/{hash} that read a local hash file
@GetMapping("/store/{hash}")
@ResponseBody
public ResponseEntity<String> read(@PathVariable String hash) {
try (java.util.Scanner scanner = new java.util.Scanner(new java.io.File("/tmp/" + hash + ".json"))) {
String body = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
return ResponseEntity.ok()
.body(body);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("Error reading file: " + e.getMessage());
}
}
}