Class BlazingResponse

java.lang.Object
blazing.BlazingResponse

public class BlazingResponse extends Object
The response used to respond to the client's requests. Contains information such as query parameters.
 
 @Route
 public static void home(BlazingResponse response) {
 ....
 response.sendResponse("hello, world");
 }
 
 
The snipped above send a response with the text `Hello, World` to the client that made the request to `/`.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Helper method for setting up the open CORS for all clients.
    Map<String,byte[]>
    Holds the files parsed from a multipart/form-data form
     
    boolean
    When you make requests with non-simple HTTP methods (e.g., POST, PUT, DELETE), or if you’re sending custom headers (e.g., Authorization), the browser sends an OPTIONS request (preflight) first.
    Predefined response for the preflight, which sends a 204 header with no body to the client as the Preflight response
    Query parameters can be passed with the request without modifying the request path.
    sendResponse(int statusCode, byte[] responseBytes)
    Sends a response along with the an array of bytes as a response to a request.
    void
    sendResponse(int status, String data)
    Responds to the client with text data attached to a status code.
    void
    Responds to the client with text data.
    void
    sendStatus(int status)
    Sends a status message to the client
    void
    Responds to the client with valid mark up that is ready for client side rendering.
    void
    setHeader(String key, String value)
    Sets response headers for client access.
    void
    Streams a file to the client for download purposes, the default content type is "application/octet-stream" response.streamFile("Vid.mp4", (outstream, file) -> { try (FileInputStream fileInputStream = new FileInputStream(file)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { outstream.write(buffer, 0, bytesRead); } outstream.flush(); } catch (FileNotFoundException ex) { BlazingLog.severe(ex.getMessage()); } catch (IOException ex) { BlazingLog.severe(ex.getMessage()); } });
    void
    streamFile(String filename, String stream_type, BlazingStreamFunction callback)
    Streams a file to the client for download purposes response.streamFile("Archive.zip", "application/zip", (outstream, file) -> { try (FileInputStream fileInputStream = new FileInputStream(file)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { outstream.write(buffer, 0, bytesRead); } outstream.flush(); } catch (FileNotFoundException ex) { BlazingLog.severe(ex.getMessage()); } catch (IOException ex) { BlazingLog.severe(ex.getMessage()); } });
    void
    Streams a file with a size that is known upfront.
    void
    streamSizedFile(String filename, String stream_type, BlazingStreamFunction callback)
    Streams a file with a size that is known upfront.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BlazingResponse

      public BlazingResponse(HttpExchange exchange)
  • Method Details

    • getRequestURI

      public URI getRequestURI()
    • params

      public Map<String,String> params()
      Query parameters can be passed with the request without modifying the request path. Parameters are stored in the response object.
       
       @Route("/users")
       public static void home(BlazingResponse response) {
       	Map<String, String> params = response.params();
       	String name = params.get("name");
       	if (users.contains(name)) {
       		....
       	}
       }
       
       
      Returns:
      Returns a Map of queries issued along with the request by the client.
    • files

      public Map<String,byte[]> files()
      Holds the files parsed from a multipart/form-data form
       
       @Post("/v1/post/upload")
       public static void home(BlazingResponse response) {
       	Map<String, byte[]> files = response.files();
       }
       
       
      Returns:
      Returns a Map of file names and their binary representations as binary data
    • sendUiRespose

      public void sendUiRespose(WebXElement root)
      Responds to the client with valid mark up that is ready for client side rendering.
       
       	response.sendUiResponse(
       		new P("Hello, world") // <p>Hello, world</p>
       	);
       
       
      The snipped above response with the Paragraph element with inner text set.
      Parameters:
      root - The root node of the UI response.
    • sendResponse

      public void sendResponse(String data)
      Responds to the client with text data.
       
       		response.sendResponse("hello 1234");
       
       
      The snipped above response with text data.
      Parameters:
      data - The actual data being sent to the client.
    • sendResponse

      public void sendResponse(int status, String data)
      Responds to the client with text data attached to a status code.
       
       		response.sendResponse(404, "hello 1234");
       
       
      The snipped above response with text data.
      Parameters:
      status - The status code sent to the client
      data - The actual data being sent to the client.
    • setHeader

      public void setHeader(String key, String value)
      Sets response headers for client access. Useful when using BlazingWebX for backend data APIs and clients have to access the data from different ports or domain.
      Parameters:
      key -
      value -
    • isPreFlight

      public boolean isPreFlight()
      When you make requests with non-simple HTTP methods (e.g., POST, PUT, DELETE), or if you’re sending custom headers (e.g., Authorization), the browser sends an OPTIONS request (preflight) first. The server needs to respond to this with appropriate CORS headers.
      Returns:
    • noContent

      public Result<Boolean,IOException> noContent()
      Predefined response for the preflight, which sends a 204 header with no body to the client as the Preflight response
      Returns:
    • defaultCORSHeaders

      public void defaultCORSHeaders()
      Helper method for setting up the open CORS for all clients. NOTE: Please use this method when testing. When deploying for production please use setHeader and set the appropriate CORS
    • sendResponse

      public Result<Boolean,IOException> sendResponse(int statusCode, byte[] responseBytes)
      Sends a response along with the an array of bytes as a response to a request.
      Parameters:
      statusCode -
      responseBytes -
      Returns:
    • sendStatus

      public void sendStatus(int status)
      Sends a status message to the client
       
       		response.sendStatus(404);
       
       
      Parameters:
      status - The status code sent to the client
    • streamFile

      public void streamFile(String filename, BlazingStreamFunction callback)
      Streams a file to the client for download purposes, the default content type is "application/octet-stream" response.streamFile("Vid.mp4", (outstream, file) -> { try (FileInputStream fileInputStream = new FileInputStream(file)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { outstream.write(buffer, 0, bytesRead); } outstream.flush(); } catch (FileNotFoundException ex) { BlazingLog.severe(ex.getMessage()); } catch (IOException ex) { BlazingLog.severe(ex.getMessage()); } });
      Parameters:
      filename -
      callback -
    • streamFile

      public void streamFile(String filename, String stream_type, BlazingStreamFunction callback)
      Streams a file to the client for download purposes response.streamFile("Archive.zip", "application/zip", (outstream, file) -> { try (FileInputStream fileInputStream = new FileInputStream(file)) { byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { outstream.write(buffer, 0, bytesRead); } outstream.flush(); } catch (FileNotFoundException ex) { BlazingLog.severe(ex.getMessage()); } catch (IOException ex) { BlazingLog.severe(ex.getMessage()); } });
      Parameters:
      filename -
      stream_type -
      callback -
    • streamSizedFile

      public void streamSizedFile(String filename, BlazingStreamFunction callback)
      Streams a file with a size that is known upfront. This is useful for sending small files such as .pdf documents, etc. Sets the content type to "application/octet-stream" as the default value
      Parameters:
      filename -
      callback -
    • streamSizedFile

      public void streamSizedFile(String filename, String stream_type, BlazingStreamFunction callback)
      Streams a file with a size that is known upfront. This is useful for sending small files such as .pdf documents, etc.
      Parameters:
      filename -
      stream_type -
      callback -