package com.github.bot.curiosone.api;

import spark.Request;
import spark.Response;

/**
 * Utility class to manage CORS mechanism.
 * Provides methods to set headers in Request objects.
 */
public class CorsUtil {

  /**
   * String representation of HTTP Origin header.
   */
  private static final String ORIGINS = "*";

  /**
   * String representation of HTTP Methods for RESTful Services.
   */
  private static final String METHODS = "GET, PUT, POST, DELETE, HEAD";

  /**
   * String representation of HTTP headers.
   */
  private static final String HEADERS = "";

  /**
  * Default Constructor.
  */
  private CorsUtil() {}

  /**
   * Sets headers for CORS options requests.
   * @param req The request object.
   * @param res The response object.
   * @return The response body.
   */
  public static String options(Request req, Response res) {
    String accessControlRequestHeaders = req.headers("Access-Control-Request-Headers");
    if (accessControlRequestHeaders != null) {
      res.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
    }

    String accessControlRequestMethod = req.headers("Access-Control-Request-Method");
    if (accessControlRequestMethod != null) {
      res.header("Access-Control-Allow-Methods", accessControlRequestMethod);
    }

    return "OK";
  }

  /**
   * Sets headers for CORS options requests.
   * @param req The request object.
   * @param res The response object.
   */
  public static void headers(Request req, Response res) {
    res.header("Access-Control-Allow-Origin", ORIGINS);
    res.header("Access-Control-Request-Method", METHODS);
    res.header("Access-Control-Allow-Headers", HEADERS);
  }
}

@@ -93,17 +93,17 @@
 Utility 
-c
+C
 lass to 
@@ -692,33 +692,47 @@
  @param 
+ 
 req
+%0A   *        
  The 
-r
+R
 equest 
-o
+O
 bject.%0A 
@@ -742,33 +742,47 @@
  @param 
+ 
 res
+%0A   *        
  The 
-r
+R
 esponse 
 object.%0A
@@ -765,33 +765,33 @@
    The Response 
-o
+O
 bject.%0A   * @ret
@@ -794,16 +794,17 @@
 @return 
+ 
 The resp
@@ -814,16 +814,155 @@
 e body.%0A
+   * @see  %3Ca href=%22https://goo.gl/T8LFRm%22%3ESpark Request Javadoc%3C/a%3E%0A   * @see  %3Ca href=%22https://goo.gl/nFCekX%22%3ESpark Response Javadoc%3C/a%3E%0A
    */%0A  
@@ -1512,33 +1512,33 @@
 or CORS options 
-r
+R
 equests.%0A   * @p
@@ -1546,25 +1546,39 @@
 ram 
+ 
 req
+%0A   *        
  The 
-r
+R
 equest 
-o
+O
 bjec
@@ -1596,25 +1596,39 @@
 ram 
+ 
 res
+%0A   *        
  The 
-r
+R
 esponse 
 obje
@@ -1627,16 +1627,155 @@
 nse 
-o
+O
 bject.%0A
+   * @see  %3Ca href=%22https://goo.gl/T8LFRm%22%3ESpark Request Javadoc%3C/a%3E%0A   * @see  %3Ca href=%22https://goo.gl/nFCekX%22%3ESpark Response Javadoc%3C/a%3E%0A
    *

improve javadoc layout CorsUtil