Package us.monoid.web

Source Code of us.monoid.web.AbstractResource

/**
*
*/
package us.monoid.web;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLConnection;

/**
* Abstract base class for all resource handlers you want to use with Resty.
*
* It gives access to the underlying URLConnection and the current inputStream
*
* @author beders
*
*/
public abstract class AbstractResource extends Resty {
  protected URLConnection urlConnection;
  protected InputStream inputStream;

  abstract String getAcceptedTypes();
 
  void fill(URLConnection anUrlConnection) throws IOException {
    urlConnection = anUrlConnection;
    inputStream = anUrlConnection.getInputStream();
  }

  public URLConnection getUrlConnection() {
    return urlConnection;
  }

  public HttpURLConnection http() {
    return (HttpURLConnection)urlConnection;
  }

  public InputStream stream() {
    return inputStream;
  }
 
  /**
   * Check if the URLConnection has returned the specified responseCode
   *
   * @param responseCode
   * @return
   */
  public boolean status(int responseCode) {
    if (urlConnection instanceof HttpURLConnection) {
      HttpURLConnection http = (HttpURLConnection) urlConnection;
      try {
        return http.getResponseCode() == responseCode;
      } catch (IOException e) {
        e.printStackTrace();
        return false;
      }
    } else
      return false;
  }

}
TOP

Related Classes of us.monoid.web.AbstractResource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.