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

org.whispersystems.signalservice.internal.websocket.WebsocketResponse Maven / Gradle / Ivy

There is a newer version: 2.15.3_unofficial_107
Show newest version
package org.whispersystems.signalservice.internal.websocket;



import org.whispersystems.signalservice.api.util.Preconditions;

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

public class WebsocketResponse {
  private final int                 status;
  private final String              body;
  private final Map headers;
  private final boolean             unidentified;

  WebsocketResponse(int status, String body, List headers, boolean unidentified) {
    this(status, body, parseHeaders(headers), unidentified);
  }

  WebsocketResponse(int status, String body, Map headerMap, boolean unidentified) {
    this.status       = status;
    this.body         = body;
    this.headers      = headerMap;
    this.unidentified = unidentified;
  }

  public int getStatus() {
    return status;
  }

  public String getBody() {
    return body;
  }

  public String getHeader(String key) {
    return headers.get(Preconditions.checkNotNull(key.toLowerCase()));
  }

  public boolean isUnidentified() {
    return unidentified;
  }

  private static Map parseHeaders(List rawHeaders) {
    Map headers = new HashMap<>(rawHeaders.size());

    for (String raw : rawHeaders) {
      if (raw != null && !raw.isEmpty()) {
        int colonIndex = raw.indexOf(":");

        if (colonIndex > 0 && colonIndex < raw.length() - 1) {
          String key   = raw.substring(0, colonIndex).trim().toLowerCase();
          String value = raw.substring(colonIndex + 1).trim();

          headers.put(key, value);
        }
      }
    }

    return headers;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy