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

📄 collectionservice.java

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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.jeffhanson.rest.core.Representation;
import com.jeffhanson.rest.core.Request;
import com.jeffhanson.rest.server.BusinessService;
import com.jeffhanson.rest.server.ServiceExecutionException;
import com.jeffhanson.rest.server.http.HTTPRequest;

public class CollectionService
    implements BusinessService
{
  // todo: read RESOURCES_DIR from config or system property
  //
  private static final String RESOURCES_DIR = "resources";

  private String contextRootPath = "";

  private String contextPath = "";

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

  protected String getContextPath()
  {
    return contextPath;
  }

  protected String filePathFromRequest(Request httpReq)
  {
    return contextRootPath + RESOURCES_DIR
           + ((HTTPRequest) httpReq).getRequest().getPathInfo();
  }

  protected void writeToFile(Request request,
                             File file,
                             boolean append)
      throws IOException,
        FileNotFoundException
  {
    System.out.println("writeToFile called");

    InputStream inStream = ((HTTPRequest) request).getInputStream();
    byte[] dataBuf = new byte[4096];

    FileOutputStream outStream = new FileOutputStream(file, append);
    int bytesRead = 0;
    while ((bytesRead = inStream.read(dataBuf)) > 0)
    {
      System.out.println("Writing [" + bytesRead + " bytes]");
      outStream.write(dataBuf, 0, bytesRead);
    }

    outStream.flush();
    outStream.close();
  }

  public Representation create(Request request)
      throws ServiceExecutionException
  {
    System.out.println("CollectionService.create()");

    // todo: create feed from structured storage with DAO
    //
    String filePath = filePathFromRequest(request);
    File file = new File(filePath);

    try
    {
      boolean append = false;
      writeToFile(request, file, append);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw new ServiceExecutionException(e);
    }

    FeedRepresentation representation = new FeedRepresentation(file);

    return representation;
  }

  public Representation read(Request request)
      throws ServiceExecutionException
  {
    System.out.println("CollectionService.read()");

    // todo: read feed from structured storage with DAO
    //
    String filePath = filePathFromRequest(request);
    File file = new File(filePath);
    if (file.exists() == false)
    {
      throw new ServiceExecutionException("Feed file ["
                                          + filePath + "] does not exist.");
    }

    FeedRepresentation representation = new FeedRepresentation(file);

    return representation;
  }

  public Representation update(Request request)
      throws ServiceExecutionException
  {
    System.out.println("CollectionService.update()");

    // todo: update feed from structured storage with DAO
    //
    String filePath = filePathFromRequest(request);
    File file = new File(filePath);
    FeedRepresentation representation = new FeedRepresentation(file);

    try
    {
      boolean append = (file.exists() ? true : false);
      writeToFile(request, file, append);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw new ServiceExecutionException(e);
    }

    return representation;
  }

  public void delete(Request request)
      throws ServiceExecutionException
  {
    System.out.println("CollectionService.delete()");

    // todo: delete feed from structured storage with DAO
    //
    String filePath = filePathFromRequest(request);
    System.out.println("CollectionService.delete() - resolving feed file [" + filePath + "]");
    File file = new File(filePath);
    System.out.println("CollectionService.delete() - feed file resolved");
    if (file.exists())
    {
      System.out.println("CollectionService.delete() - deleting feed file");

      if (file.delete() == false)
      {
        System.out.println("CollectionService.delete() - feed deletion failed");
        throw new ServiceExecutionException("Error deleting feed ["
                                            + filePath + "]");
      }
      System.out.println("CollectionService.delete() - feed deletion succeeded");
    }
    else
    {
      System.out.println("CollectionService.delete() - feed file ["
                         + filePath + "] does not exist");
    }
  }
}

⌨️ 快捷键说明

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