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

le-annen.javaserver.0.1.source-code.SendResponse Maven / Gradle / Ivy

Go to download

A minimal java http server which fulfills small portions of the http specifications. This is not a full fledged or secure server. It should only be used for learning purposes and contains no guarantees of any king.

The newest version!
import java.io.*;
import java.net.Socket;

public class SendResponse implements SendInterface {

  @Override
  public void send(ResponseParameters responseParameters, Socket socket) throws IOException {
    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
    DataOutputStream outputStream =
            new DataOutputStream(bufferedOutputStream);
    String httpHeader = buildHeader(responseParameters);

    outputStream.writeBytes(httpHeader);

    if(responseParameters.getBodyType().equals("text")) {
      outputStream.writeBytes(responseParameters.getBodyContent());
    } else if(responseParameters.getBodyType().equals("file")) {
      File file = new File(responseParameters.getBodyContent());
      FileInputStream fileInputStream = new FileInputStream(file);
      BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

      byte[] buffer = new byte[8192];
      int available = -1;
      while((available = bufferedInputStream.read(buffer)) > 0) {
        bufferedOutputStream.write(buffer, 0, available);
        bufferedOutputStream.flush();
      }
      outputStream.writeBytes("\r\n\r\n");
    }
    outputStream.flush();
  }

  String buildHeader(ResponseParameters responseParameters) {
    String lineEnding = "\r\n";
    return responseParameters.getResponseStatus() +
            responseParameters.getDate() +
            responseParameters.getContentDisposition() +
            responseParameters.getContentLength() +
            responseParameters.getContentType() +
            responseParameters.getConnectionClose() +
            lineEnding;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy