org.simpleframework.http.Request类的使用及代码示例

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

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

Request介绍

[英]The Request is used to provide an interface to the HTTP entity body and message header. This provides methods that allow the entity body to be acquired as a stream, string, or if the message is a multipart encoded body, then the individual parts of the request body can be acquired.

This can also maintain data during the request lifecycle as well as the session lifecycle. A Session is made available for convenience. It provides a means for the services to associate data with a given client session, which can be retrieved when there are subsequent requests sent to the server.

It is important to note that the entity body can be read multiple times from the request. Calling getInputStream will start reading from the first byte in the body regardless of the number of times it is called. This allows POST parameters as well as multipart bodies to be read from the stream if desired.
[中]Request用于为HTTP实体主体和消息头提供接口。这提供了一些方法,允许以流、字符串的形式获取实体主体,或者如果消息是多部分编码的主体,则可以获取请求主体的各个部分。
这还可以在请求生命周期和会话生命周期中维护数据。为方便起见,我们提供了Session。它为服务提供了一种将数据与给定客户机会话关联的方法,当有后续请求发送到服务器时,可以检索到该会话。
需要注意的是,可以从请求中多次读取实体体。调用getInputStream将从正文中的第一个字节开始读取,无论调用了多少次。这允许在需要时从流中读取POST参数和多部分实体。

代码示例

代码示例来源:origin: mpetazzoni/ttorrent

public void handle(Request request, final Response response) {
 if (!Tracker.ANNOUNCE_URL.equals(request.getPath().toString())) {
  response.setCode(404);
  response.setText("Not Found");
  response.setDate("Date", System.currentTimeMillis());
  if ("GET".equalsIgnoreCase(request.getMethod())) {
   myRequestProcessor.process(request.getAddress().toString(), request.getClientAddress().getAddress().getHostAddress(),
       getRequestHandler(response));
  } else {
   myMultiAnnounceRequestProcessor.process(request.getContent(), request.getAddress().toString(),
       request.getClientAddress().getAddress().getHostAddress(), getRequestHandler(response));

代码示例来源:origin: jersey/jersey

@Override
public void handle(final Request request, final Response response) {
  final ResponseWriter responseWriter = new ResponseWriter(response, scheduler);
  final URI baseUri = getBaseUri(request);
  final URI requestUri = getRequestUri(request, baseUri);
  try {
    final ContainerRequest requestContext = new ContainerRequest(baseUri, requestUri,
        request.getMethod(), getSecurityContext(request), new MapPropertiesDelegate());
    requestContext.setEntityStream(request.getInputStream());
    for (final String headerName : request.getNames()) {
      requestContext.headers(headerName, request.getValue(headerName));
    }
    requestContext.setWriter(responseWriter);
    requestContext.setRequestScopedInitializer(injectionManager -> {
      injectionManager.<Ref<Request>>getInstance(RequestTYPE).set(request);
      injectionManager.<Ref<Response>>getInstance(ResponseTYPE).set(response);
    });
    appHandler.handle(requestContext);
  } catch (final Exception ex) {
    throw new RuntimeException(ex);
  } finally {
    if (!responseWriter.isSuspended()) {
      close(response);
    }
  }
}

代码示例来源:origin: jersey/jersey

private URI getRequestUri(final Request request, final URI baseUri) {
  try {
    final String serverAddress = getServerAddress(baseUri);
    String uri = ContainerUtils.getHandlerPath(request.getTarget());
    final String queryString = request.getQuery().toString();
    if (queryString != null) {
      uri = uri + "?" + ContainerUtils.encodeUnsafeCharacters(queryString);
    }
    return new URI(serverAddress + uri);
  } catch (URISyntaxException ex) {
    throw new IllegalArgumentException(ex);
  }
}

代码示例来源:origin: jersey/jersey

private URI getBaseUri(final Request request) {
  try {
    final String hostHeader = request.getValue("Host");
    if (hostHeader != null) {
      final String scheme = request.isSecure() ? "https" : "http";
      return new URI(scheme + "://" + hostHeader + "/");
    } else {
      final Address address = request.getAddress();
      return new URI(address.getScheme(), null, address.getDomain(), address.getPort(), "/", null,
          null);
    }
  } catch (final URISyntaxException ex) {
    throw new IllegalArgumentException(ex);
  }
}

代码示例来源:origin: kristofa/mock-http-server

public static FullHttpRequest convert(final Request request) {
  byte[] data = null;
  try {
    final InputStream inputStream = request.getInputStream();
    try {
      data = IOUtils.toByteArray(inputStream);
    } finally {
      inputStream.close();
    }
  } catch (final IOException e) {
    LOGGER.error("IOException when getting request content.", e);
  }
  final FullHttpRequestImpl httpRequest = new FullHttpRequestImpl();
  httpRequest.domain(request.getAddress().getDomain());
  httpRequest.port(request.getAddress().getPort());
  httpRequest.method(Method.valueOf(request.getMethod()));
  httpRequest.path(request.getPath().getPath());
  if (data.length > 0) {
    httpRequest.content(data);
  }
  for (final String headerField : request.getNames()) {
    for (final String headerFieldValue : request.getValues(headerField)) {
      httpRequest.httpMessageHeader(headerField, headerFieldValue);
    }
  }
  for (final Entry<String, String> entry : request.getQuery().entrySet()) {
    httpRequest.queryParameter(entry.getKey(), entry.getValue());
  }
  return httpRequest;
}

代码示例来源:origin: SonarSource/sonarqube

public void handle(Request req, Response resp) {
 try {
  if (req.getPath().getPath().contains("/redirect/")) {
   resp.setCode(303);
   resp.add("Location", "/");
  } else {
   if (req.getPath().getPath().contains("/timeout/")) {
    try {
     Thread.sleep(500);
   if (req.getPath().getPath().contains("/gzip/")) {
    if (!"gzip".equals(req.getValue("Accept-Encoding"))) {
     throw new IllegalStateException("Should accept gzip");
    gzipOutputStream.close();
   } else {
    resp.getPrintStream().append("agent=" + req.getValues("User-Agent").get(0));

代码示例来源:origin: ngallagher/simpleframework

/**
* This can be used to get the HTTP method for this request. The
* HTTP specification RFC 2616 specifies the HTTP request methods
* in section 9, Method Definitions. Typically this will be a
* GET, POST or a HEAD method, although any string is possible.
*
* @return the request method for this request message
*/ 
public String getMethod() {
 return request.getMethod();
}

代码示例来源:origin: miltonio/milton2

public Map<String, String> getHeaders() {
  Map<String, String> headers = new HashMap<String, String>();
  for (String s : baseRequest.getNames()) {
    String val = baseRequest.getValue(s);
    headers.put(s, val);
  }
  return headers;
}

代码示例来源:origin: miltonio/milton2

@Override
public String getAbsoluteUrl() {
  String s = baseRequest.getTarget();
    return s;
  } else {
    String host = baseRequest.getValue("Host");
    Address a = baseRequest.getAddress();
    if (host == null) {
      host = a.getDomain();
    if (baseRequest.isSecure()) {
      s = "https";
    } else {
      s = s + ":" + a.getPort();
    s = s + baseRequest.getTarget();

代码示例来源:origin: org.restlet/org.restlet.ext.simple

@Override
public InputStream getRequestEntityStream(long size) {
  try {
    return this.request.getInputStream();
  } catch (Exception ex) {
    return null;
  }
}

代码示例来源:origin: com.sun.jersey.contribs/jersey-simple-server

public void handle(Request request, Response response) {
  WebApplication target = application;
  final URI baseUri = getBaseUri(request);
  final URI requestUri = baseUri.resolve(request.getTarget());
  try {
    final ContainerRequest cRequest = new ContainerRequest(
        target,
        request.getMethod(),
        baseUri,
        requestUri,
        getHeaders(request),
        request.getInputStream());
    target.handleRequest(cRequest, new Writer(request, response));
  } catch (Exception ex) {
    throw new RuntimeException(ex);
  } finally {
    close(response);
  }
}

代码示例来源:origin: org.kie.remote/kie-remote-client

public void handle( Request req, Response resp ) {
  try {
    PrintStream out = resp.getPrintStream(1024);
    String address = req.getAddress().toString();
    if( address.equals(DEFAULT_ENPOINT) ) {
      String content = readInputStreamAsString(req.getInputStream());
      JaxbCommandsRequest cmdsReq = (JaxbCommandsRequest) jaxbSerializationProvider.deserialize(content);
      String [] headerNames = {
          TEST_HEADER_NAME,
          ANOTHER_TEST_HEADER_NAME,
          NOT_SENT_HEADER_NAME
      };
      List<String> headerValues = new ArrayList<String>();
      for( String headerName : headerNames ) {
        String headerVal = req.getValue(headerName);
        if( headerVal != null ) {
          headerValues.add(headerVal);
        }
      }
      String output = handleJaxbCommandsRequest(cmdsReq, headerValues);
      resp.setCode(HttpURLConnection.HTTP_OK);
      out.print(output);
    } else {
      resp.setCode(HttpURLConnection.HTTP_BAD_REQUEST);
    }
    out.close();
  } catch( Exception e ) {
    e.printStackTrace();
  }
}

代码示例来源:origin: opendedup/sdfs

Path reqPath = request.getPath();
String[] parts = request.getTarget().split("\\?");
Map<String, String> qry = null;
if (parts.length > 1) {
if (request.getTarget().startsWith(SESSION)) {
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder;
  sessions.put(sessionid, request.getClientAddress().getHostString());
  Element result = doc.getDocumentElement();
  result.setAttribute("status", "success");
      byte[] rb = com.google.common.io.BaseEncoding.base64Url().decode(request.getParameter("data"));
      byte[] rslt = new BatchGetBlocksCmd().getResult(rb);
      long time = System.currentTimeMillis();
    SDFSLogger.getLog().error("invalid path " + reqPath.getPath());	
    body.close();
  } else if (request.getTarget().startsWith(IO_PATH)) {
    String pth = request.getTarget().substring(IO_PATH.length()).split("\\?")[0];
  } else if (Main.matcher != null && request.getTarget().startsWith(Main.matcher.getWPath())) {
    Main.matcher.getResult(request.getTarget(), 0, response);
  } else if (request.getTarget().startsWith(METADATA_PATH)) {
    long time = System.currentTimeMillis();
    response.setDate("Date", time);
    response.setDate("Last-Modified", time);

代码示例来源:origin: miltonio/milton2

@Override
public String getRequestHeader(Header header) {
  return baseRequest.getValue(header.code);
}

代码示例来源:origin: org.simpleframework/simple

/**
* This method is used to acquire the query part from the HTTP 
* request URI target and a form post if it exists. Both the 
* query and the form post are merge together in a single query.
* 
* @return the query associated with the HTTP target URI
*/ 
public Query getQuery() {
 return request.getQuery();
}

代码示例来源:origin: ngallagher/simpleframework

/**
* This can be used to get the URI specified for this HTTP request.
* This corresponds to the either the full HTTP URI or the path
* part of the URI depending on how the client sends the request.
*
* @return the URI address that this HTTP request is targeting
*/ 
public String getTarget() {
 return request.getTarget();
}

代码示例来源:origin: org.simpleframework/simple-http

/**
* This is used to acquire the path as extracted from the HTTP 
* request URI. The <code>Path</code> object that is provided by
* this method is immutable, it represents the normalized path 
* only part from the request uniform resource identifier.
* 
* @return this returns the normalized path for the request
*/
public Path getPath() {
 return request.getPath();
}

代码示例来源:origin: org.simpleframework/simple-http

/**
* This is used to acquire the remote client address. This can 
* be used to acquire both the port and the I.P address for the 
* client. It allows the connected clients to be logged and if
* require it can be used to perform course grained security.
* 
* @return this returns the client address for this request
*/
public InetSocketAddress getClientAddress() {
 return request.getClientAddress();
}

代码示例来源:origin: miltonio/milton2

@Override
public String toString() {
  return request.getMethod() + " " + request.getAddress().toString();
}

代码示例来源:origin: ngallagher/simpleframework

/**
* This method is used to get a <code>List</code> of the names
* for the headers. This will provide the original names for the
* HTTP headers for the message. Modifications to the provided
* list will not affect the header, the list is a simple copy.
*
* @return this returns a list of the names within the header
*/
public List<String> getNames() {
 return request.getNames();
}

相关文章