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

org.java_websocket.extensions.ExtensionRequestData Maven / Gradle / Ivy

Go to download

转接远程的http接口,服务器响应正确的数据格式必须是JResponse定义的格式。 客户端websocket使用"org.java-websocket:Java-WebSocket:1.5.2",项目地址https://github.com/TooTallNate/Java-WebSocket; 对Java-WebSocket做了适当修改。

The newest version!
package org.java_websocket.extensions;

import java.util.LinkedHashMap;
import java.util.Map;

public class ExtensionRequestData {

  public static final String EMPTY_VALUE = "";

  private Map extensionParameters;
  private String extensionName;

  private ExtensionRequestData() {
    extensionParameters = new LinkedHashMap<>();
  }

  public static ExtensionRequestData parseExtensionRequest(String extensionRequest) {
    ExtensionRequestData extensionData = new ExtensionRequestData();
    String[] parts = extensionRequest.split(";");
    extensionData.extensionName = parts[0].trim();

    for (int i = 1; i < parts.length; i++) {
      String[] keyValue = parts[i].split("=");
      String value = EMPTY_VALUE;

      // Some parameters don't take a value. For those that do, parse the value.
      if (keyValue.length > 1) {
        String tempValue = keyValue[1].trim();

        // If the value is wrapped in quotes, just get the data between them.
        if ((tempValue.startsWith("\"") && tempValue.endsWith("\""))
            || (tempValue.startsWith("'") && tempValue.endsWith("'"))
            && tempValue.length() > 2) {
          tempValue = tempValue.substring(1, tempValue.length() - 1);
        }

        value = tempValue;
      }

      extensionData.extensionParameters.put(keyValue[0].trim(), value);
    }

    return extensionData;
  }

  public String getExtensionName() {
    return extensionName;
  }

  public Map getExtensionParameters() {
    return extensionParameters;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy