org.wso2.msf4j.Request.getHeader()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(177)

本文整理了Java中org.wso2.msf4j.Request.getHeader方法的一些代码示例,展示了Request.getHeader的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Request.getHeader方法的具体详情如下:
包路径:org.wso2.msf4j.Request
类名称:Request
方法名:getHeader

Request.getHeader介绍

[英]Get an HTTP header of the HTTP request.
[中]获取HTTP请求的HTTP头。

代码示例

代码示例来源:origin: wso2/msf4j

@Override
public String getHttpHeaderValue(String headerName) {
  return req.getHeader(headerName);
}

代码示例来源:origin: org.wso2.msf4j/msf4j-core

/**
 * Get the request's content type.
 *
 * @return String request's content type
 */
public String getContentType() {
  return this.request.getHeader("Content-Type");
}

代码示例来源:origin: wso2/msf4j

/**
 * Get the request's content type.
 *
 * @return String request's content type
 */
public String getContentType() {
  return this.request.getHeader("Content-Type");
}

代码示例来源:origin: org.wso2.carbon.auth/org.wso2.carbon.auth.rest.api.authenticators

private String getAuthenticationType(Request request) {
  String header = request.getHeader(RestAPIConstants.AUTHORIZATION);
  if (StringUtils.isNotEmpty(header)) {
    if (header.contains("Basic")) {
      return RestAPIConstants.AUTH_TYPE_BASIC;
    } else if (header.contains("Bearer")) {
      return RestAPIConstants.AUTH_TYPE_OAUTH2;
    }
  }
  return null;
}

代码示例来源:origin: org.wso2.msf4j/msf4j-core

private void setBaseUri(Request request) {
  StringBuilder builder = new StringBuilder();
  builder.append(request.getProperty(Constants.PROTOCOL).toString().toLowerCase(Locale.US)).append("://")
      .append(request.getHeader(HttpHeaderNames.HOST.toString()));
  if (builder.charAt(builder.length() - 1) != '/') {
    builder.append("/");
  }
  try {
    MSF4JResponse.setBaseUri(new URI(builder.toString()));
  } catch (URISyntaxException e) {
    log.error("Error while setting the Base URI. " + e.getMessage(), e);
  }
}

代码示例来源:origin: wso2/msf4j

private void setBaseUri(Request request) {
  StringBuilder builder = new StringBuilder();
  builder.append(request.getProperty(Constants.PROTOCOL).toString().toLowerCase(Locale.US)).append("://")
      .append(request.getHeader(HttpHeaderNames.HOST.toString()));
  if (builder.charAt(builder.length() - 1) != '/') {
    builder.append("/");
  }
  try {
    MSF4JResponse.setBaseUri(new URI(builder.toString()));
  } catch (URISyntaxException e) {
    log.error("Error while setting the Base URI. " + e.getMessage(), e);
  }
}

代码示例来源:origin: org.wso2.msf4j/msf4j-core

@SuppressWarnings("unchecked")
private Object getCookieParamValue(HttpResourceModel.ParameterInfo<String> info, Request request) {
  CookieParam cookieParam = info.getAnnotation();
  String cookieName = cookieParam.value();
  String cookieHeader = request.getHeader("Cookie");
  if (cookieHeader != null) {
    String cookieValue = Arrays.stream(cookieHeader.split(";"))
        .filter(cookie -> cookie.startsWith(cookieName + "="))
        .findFirst()
        .map(cookie -> cookie.substring((cookieName + "=").length()))
        .orElseGet(info::getDefaultVal);
    return info.convert(cookieValue);
  }
  return null;
}

代码示例来源:origin: wso2/msf4j

@SuppressWarnings("unchecked")
private Object getCookieParamValue(HttpResourceModel.ParameterInfo<String> info, Request request) {
  CookieParam cookieParam = info.getAnnotation();
  String cookieName = cookieParam.value();
  String cookieHeader = request.getHeader("Cookie");
  if (cookieHeader != null) {
    String cookieValue = Arrays.stream(cookieHeader.split(";"))
        .filter(cookie -> cookie.startsWith(cookieName + "="))
        .findFirst()
        .map(cookie -> cookie.substring((cookieName + "=").length()))
        .orElseGet(info::getDefaultVal);
    return info.convert(cookieValue);
  }
  return null;
}

代码示例来源:origin: wso2/msf4j

@Override
public boolean interceptRequest(Request request, Response response) throws Exception {
  String authHeader = request.getHeader(javax.ws.rs.core.HttpHeaders.AUTHORIZATION);
  if (authHeader != null) {
    String authType = authHeader.substring(0, AUTH_TYPE_BASIC_LENGTH);
    String authEncoded = authHeader.substring(AUTH_TYPE_BASIC_LENGTH).trim();
    if (AUTH_TYPE_BASIC.equals(authType) && !authEncoded.isEmpty()) {
      byte[] decodedByte = authEncoded.getBytes(Charset.forName(CHARSET_UTF_8));
      String authDecoded = new String(Base64.getDecoder().decode(decodedByte),
          Charset.forName(CHARSET_UTF_8));
      String[] authParts = authDecoded.split(":");
      String username = authParts[0];
      String password = authParts[1];
      if (authenticate(username, password)) {
        return true;
      }
    }
  }
  response.setStatus(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode());
  response.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, AUTH_TYPE_BASIC);
  return false;
}

代码示例来源:origin: org.wso2.msf4j/msf4j-core

@Override
public boolean interceptRequest(Request request, Response response) throws Exception {
  String authHeader = request.getHeader(javax.ws.rs.core.HttpHeaders.AUTHORIZATION);
  if (authHeader != null) {
    String authType = authHeader.substring(0, AUTH_TYPE_BASIC_LENGTH);
    String authEncoded = authHeader.substring(AUTH_TYPE_BASIC_LENGTH).trim();
    if (AUTH_TYPE_BASIC.equals(authType) && !authEncoded.isEmpty()) {
      byte[] decodedByte = authEncoded.getBytes(Charset.forName(CHARSET_UTF_8));
      String authDecoded = new String(Base64.getDecoder().decode(decodedByte),
          Charset.forName(CHARSET_UTF_8));
      String[] authParts = authDecoded.split(":");
      String username = authParts[0];
      String password = authParts[1];
      if (authenticate(username, password)) {
        return true;
      }
    }
  }
  response.setStatus(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode());
  response.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, AUTH_TYPE_BASIC);
  return false;
}

代码示例来源:origin: wso2/msf4j

@Override
  public void postCall(Request request, int status, ServiceMethodInfo serviceMethodInfo) {
    numPostCalls.incrementAndGet();
    String header = request.getHeader("X-Request-Type");
    if (header != null && header.equals("PostException")) {
      throw new IllegalArgumentException("PostException");
    }
  }
}

代码示例来源:origin: wso2/msf4j

/**
 * Returns the current session associated with this request, or if the request does not have a session,
 * creates one.
 *
 * @return Session
 */
public Session getSession() {
  if (sessionManager == null) {
    throw new IllegalStateException("SessionManager has not been set");
  }
  if (session != null) {
    return session.setAccessed();
  }
  String cookieHeader = getHeader("Cookie");
  if (cookieHeader != null) {
    session = Arrays.stream(cookieHeader.split(";")).map(String::trim)
        .filter(cookie -> cookie.startsWith(MSF4JConstants.SESSION_ID))
        .findFirst()
        .map(jsession -> sessionManager.getSession(jsession.substring(MSF4JConstants.SESSION_ID.length())))
        .orElseGet(sessionManager::createSession);
    return session.setAccessed();
  }
  return session = sessionManager.createSession();
}

代码示例来源:origin: org.wso2.msf4j/msf4j-core

@Override
public boolean interceptRequest(Request request, Response response) throws Exception {
  log.info("Authentication precall");
  boolean isValidSignature;
  String jwtHeader = request.getHeader(JWT_HEADER);
  if (jwtHeader != null) {
    isValidSignature = verifySignature(jwtHeader);
    if (isValidSignature) {
      return true;
    }
  }
  response.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, AUTH_TYPE_JWT);
  response.setStatus(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode());
  return false;
}

代码示例来源:origin: wso2/msf4j

@Override
public boolean interceptRequest(Request request, Response response) throws Exception {
  log.info("Authentication precall");
  boolean isValidSignature;
  String jwtHeader = request.getHeader(JWT_HEADER);
  if (jwtHeader != null) {
    isValidSignature = verifySignature(jwtHeader);
    if (isValidSignature) {
      return true;
    }
  }
  response.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, AUTH_TYPE_JWT);
  response.setStatus(javax.ws.rs.core.Response.Status.UNAUTHORIZED.getStatusCode());
  return false;
}

代码示例来源:origin: wso2/msf4j

private void handleTracing(Request request, HTTPMonitoringEvent httpMonitoringEvent) {
  String traceId, parentRequest;
  if (this.isTracing()) {
    traceId = request.getHeader(ACTIVITY_ID);
    if (traceId == null) {
      traceId = this.generateTraceId();
    }
    parentRequest = request.getHeader(PARENT_REQUEST);
  } else {
    traceId = DEFAULT_TRACE_ID;
    parentRequest = DEFAULT_PARENT_REQUEST;
  }
  httpMonitoringEvent.setActivityId(traceId);
  httpMonitoringEvent.setParentRequest(parentRequest);
}

代码示例来源:origin: org.wso2.msf4j/msf4j-core

@SuppressWarnings("unchecked")
private Object getHeaderParamValue(HttpResourceModel.ParameterInfo<List<String>> info, Request request) {
  HeaderParam headerParam = info.getAnnotation();
  String headerName = headerParam.value();
  String header = request.getHeader(headerName);
  if (header == null || header.isEmpty()) {
    String defaultVal = info.getDefaultVal();
    if (defaultVal != null) {
      header = defaultVal;
    }
  }
  return info.convert(Collections.singletonList(header));
}

代码示例来源:origin: wso2/msf4j

@SuppressWarnings("unchecked")
private Object getHeaderParamValue(HttpResourceModel.ParameterInfo<List<String>> info, Request request) {
  HeaderParam headerParam = info.getAnnotation();
  String headerName = headerParam.value();
  String header = request.getHeader(headerName);
  if (header == null || header.isEmpty()) {
    String defaultVal = info.getDefaultVal();
    if (defaultVal != null) {
      header = defaultVal;
    }
  }
  return info.convert(Collections.singletonList(header));
}

代码示例来源:origin: wso2/msf4j

@Override
public boolean preCall(Request request, Response responder, ServiceMethodInfo serviceMethodInfo)
    throws Exception {
  numPreCalls.incrementAndGet();
  String header = request.getHeader("X-Request-Type");
  if (header != null && header.equals("Reject")) {
    responder.setStatus(javax.ws.rs.core.Response.Status.NOT_ACCEPTABLE.getStatusCode());
    responder.send();
    return false;
  }
  if (header != null && header.equals("PreException")) {
    throw new IllegalArgumentException("PreException");
  }
  return true;
}

代码示例来源:origin: wso2/msf4j

/**
   * Set connection header of the response object according to the
   * connection header of the request.
   *
   * @param request  HTTP request object
   * @param response HTTP response object
   */
  public static void setConnectionHeader(Request request, Response response) {
    String connection = request.getHeader(HttpHeaderNames.CONNECTION.toString());
    if (connection != null && CLOSE.equalsIgnoreCase(connection)) {
      response.setHeader(HttpHeaderNames.CONNECTION.toString(), CLOSE);
    } else {
      response.setHeader(HttpHeaderNames.CONNECTION.toString(), KEEP_ALIVE);
    }
  }
}

代码示例来源:origin: org.wso2.msf4j/msf4j-core

/**
   * Set connection header of the response object according to the
   * connection header of the request.
   *
   * @param request  HTTP request object
   * @param response HTTP response object
   */
  public static void setConnectionHeader(Request request, Response response) {
    String connection = request.getHeader(HttpHeaderNames.CONNECTION.toString());
    if (connection != null && CLOSE.equalsIgnoreCase(connection)) {
      response.setHeader(HttpHeaderNames.CONNECTION.toString(), CLOSE);
    } else {
      response.setHeader(HttpHeaderNames.CONNECTION.toString(), KEEP_ALIVE);
    }
  }
}

相关文章