⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpcontext.java

📁 编写rest实例 rest分布式框架在app中的应用
💻 JAVA
字号:
package com.jeffhanson.rest.server.http;

import com.jeffhanson.rest.core.Representation;
import com.jeffhanson.rest.core.Request;
import com.jeffhanson.rest.server.BusinessService;
import com.jeffhanson.rest.server.Context;
import com.jeffhanson.rest.server.ContextRequestException;
import com.jeffhanson.rest.server.ServiceExecutionException;
import com.jeffhanson.rest.server.ServiceLocator;
import com.jeffhanson.rest.server.ServiceLocatorException;

public class HTTPContext
  implements Context
{
  private String contextRootPath = "";
  private String contextPath = "";

  public HTTPContext(String contextRootPath, String contextPath)
  {
    this.contextRootPath = contextRootPath;
    this.contextPath = contextPath;
  }

  public String getContextRootPath()
  {
    return contextRootPath;
  }
  
  public String getContextPath()
  {
    return contextPath;
  }
  
  protected String uriFromRequest(HTTPRequest httpReq)
  {
    String reqURI = httpReq.getRequest().getRequestURI();
    if (reqURI.startsWith(contextPath))
    {
      reqURI = reqURI.substring(contextPath.length());
    }
    
    return reqURI;
  }

  protected BusinessService resolveBusinessService(HTTPRequest httpReq)
    throws ContextRequestException
  {
    String uri = uriFromRequest(httpReq);
    
    try
    {
      BusinessService businessService = ServiceLocator.locateService(contextRootPath, contextPath, uri);
      return businessService;
    }
    catch (ServiceLocatorException e)
    {
      e.printStackTrace();
      throw new ContextRequestException(e);
    }
  }
  
  public void handleDelete(Request request)
    throws ContextRequestException
  {
    BusinessService businessService = resolveBusinessService((HTTPRequest)request);
    
    try
    {
      businessService.delete(request);
    }
    catch (ServiceExecutionException e)
    {
      e.printStackTrace();
      throw new ContextRequestException(e);
    }
  }

  public Representation handleGet(Request request)
    throws ContextRequestException
  {
    BusinessService businessService = resolveBusinessService((HTTPRequest)request);
    Representation representation = null;
    
    try
    {
      representation = businessService.read(request);
    }
    catch (ServiceExecutionException e)
    {
      e.printStackTrace();
      throw new ContextRequestException(e);
    }
    
    return representation;
  }

  public void handlePost(Request request)
    throws ContextRequestException
  {
    BusinessService businessService = resolveBusinessService((HTTPRequest)request);
    
    try
    {
      businessService.update(request);
    }
    catch (ServiceExecutionException e)
    {
      e.printStackTrace();
      throw new ContextRequestException(e);
    }
  }

  public void handlePut(Request request)
    throws ContextRequestException
  {
    try
    {
      BusinessService businessService = resolveBusinessService((HTTPRequest)request);
      businessService.update(request);
    }
    catch (ServiceExecutionException e)
    {
      e.printStackTrace();
      throw new ContextRequestException(e);
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -