Package org.servalproject.servaldna

Examples of org.servalproject.servaldna.ServalDInterfaceException


        status.input_stream = conn.getInputStream();
        return status;
      }
    }
    if (!conn.getContentType().equals("application/json"))
      throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
    if (status.http_status_code >= 300) {
      JSONTokeniser json = new JSONTokeniser(new InputStreamReader(conn.getErrorStream(), "US-ASCII"));
      decodeRestfulStatus(status, json);
    }
    if (status.http_status_code == HttpURLConnection.HTTP_FORBIDDEN)
      return status;
    if (status.http_status_code == HttpURLConnection.HTTP_NOT_IMPLEMENTED)
      throw new ServalDNotImplementedException(status.http_status_message);
    throw new ServalDInterfaceException("unexpected HTTP response: " + status.http_status_code + " " + status.http_status_message);
  }
View Full Code Here


    throw new ServalDInterfaceException("unexpected HTTP response: " + status.http_status_code + " " + status.http_status_message);
  }

  protected static ServalDInterfaceException unexpectedResponse(HttpURLConnection conn, Status status)
  {
    return new ServalDInterfaceException(
        "unexpected Rhizome failure, " + quoteString(status.http_status_message)
        + (status.bundle_status_code == null ? "" : ", " + status.bundle_status_code)
        + (status.bundle_status_message == null ? "" : " " + quoteString(status.bundle_status_message))
        + (status.payload_status_code == null ? "" : ", " + status.payload_status_code)
        + (status.payload_status_message == null ? "" : " " + quoteString(status.payload_status_message))
View Full Code Here

  protected static JSONTokeniser receiveRestfulResponse(HttpURLConnection conn, int[] expected_response_codes) throws IOException, ServalDInterfaceException
  {
    Status status = receiveResponse(conn, expected_response_codes);
    if (!conn.getContentType().equals("application/json"))
      throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
    return new JSONTokeniser(new InputStreamReader(status.input_stream, "US-ASCII"));
  }
View Full Code Here

      int hs = json.consume(Integer.class);
      json.consume(JSONTokeniser.Token.COMMA);
      if (status.http_status_code == 0)
        status.http_status_code = json.consume(Integer.class);
      else if (hs != status.http_status_code)
        throw new ServalDInterfaceException("JSON/header conflict"
            + ", http_status_code=" + hs
            + " but HTTP response code is " + status.http_status_code);
      json.consume("http_status_message");
      json.consume(JSONTokeniser.Token.COLON);
      status.http_status_message = json.consume(String.class);
      Object tok = json.nextToken();
      while (tok == JSONTokeniser.Token.COMMA) {
        String label = json.consume(String.class);
        json.consume(JSONTokeniser.Token.COLON);
        if (label.equals("rhizome_bundle_status_code")) {
          RhizomeBundleStatus bs = RhizomeBundleStatus.fromCode(json.consume(Integer.class));
          if (status.bundle_status_code == null)
            status.bundle_status_code = bs;
          else if (status.bundle_status_code != bs)
            throw new ServalDInterfaceException("JSON/header conflict"
                + ", rhizome_bundle_status_code=" + bs.code
                + " but Serval-Rhizome-Result-Bundle-Status-Code: " + status.bundle_status_code.code);
        }
        else if (label.equals("rhizome_bundle_status_message")) {
          String message = json.consume(String.class);
          if (status.bundle_status_message == null)
            status.bundle_status_message = message;
          else if (!status.bundle_status_message.equals(message))
            throw new ServalDInterfaceException("JSON/header conflict"
                + ", rhizome_bundle_status_message=" + message
                + " but Serval-Rhizome-Result-Bundle-Status-Message: " + status.bundle_status_message);
        }
        else if (label.equals("rhizome_payload_status_code")) {
          RhizomePayloadStatus bs = RhizomePayloadStatus.fromCode(json.consume(Integer.class));
          if (status.payload_status_code == null)
            status.payload_status_code = bs;
          else if (status.payload_status_code != bs)
            throw new ServalDInterfaceException("JSON/header conflict"
                + ", rhizome_payload_status_code=" + bs.code
                + " but Serval-Rhizome-Result-Payload-Status-Code: " + status.payload_status_code.code);
        }
        else if (label.equals("rhizome_payload_status_message")) {
          String message = json.consume(String.class);
          if (status.payload_status_message == null)
            status.payload_status_message = message;
          else if (!status.payload_status_message.equals(message))
            throw new ServalDInterfaceException("JSON/header conflict"
                + ", rhizome_payload_status_message=" + message
                + " but Serval-Rhizome-Result-Payload-Status-Code: " + status.payload_status_message);
        }
        else
          json.unexpected(label);
        tok = json.nextToken();
      }
      json.match(tok, JSONTokeniser.Token.END_OBJECT);
      json.consume(JSONTokeniser.Token.EOF);
    }
    catch (JSONInputException e) {
      throw new ServalDInterfaceException("malformed JSON status response", e);
    }
  }
View Full Code Here

      switch (status.bundle_status_code) {
      case NEW:
        return null;
      case SAME:
        if (!conn.getContentType().equals("rhizome-manifest/text"))
          throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
        RhizomeManifest manifest = RhizomeManifest.fromTextFormat(status.input_stream);
        BundleExtra extra = bundleExtraFromHeaders(conn);
        return new RhizomeManifestBundle(manifest, extra.rowId, extra.insertTime, extra.author, extra.secret);
      case ERROR:
        throw new ServalDFailureException("received rhizome_bundle_status_code=ERROR(-1) from " + conn.getURL());
      }
    }
    catch (RhizomeManifestParseException e) {
      throw new ServalDInterfaceException("malformed manifest from daemon", e);
    }
    finally {
      if (status.input_stream != null)
        status.input_stream.close();
    }
View Full Code Here

            status.input_stream = null;
          }
          // FALL THROUGH
        case STORED: {
            if (status.input_stream != null && !conn.getContentType().equals("application/octet-stream"))
              throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
            RhizomeManifest manifest = manifestFromHeaders(conn);
            BundleExtra extra = bundleExtraFromHeaders(conn);
            RhizomePayloadRawBundle ret = new RhizomePayloadRawBundle(manifest, status.input_stream, extra.rowId, extra.insertTime, extra.author, extra.secret);
            status.input_stream = null; // don't close when we return
            return ret;
View Full Code Here

            status.input_stream = null;
          }
          // FALL THROUGH
        case STORED: {
            if (status.input_stream != null && !conn.getContentType().equals("application/octet-stream"))
              throw new ServalDInterfaceException("unexpected HTTP Content-Type: " + conn.getContentType());
            RhizomeManifest manifest = manifestFromHeaders(conn);
            BundleExtra extra = bundleExtraFromHeaders(conn);
            RhizomePayloadBundle ret = new RhizomePayloadBundle(manifest, status.input_stream, extra.rowId, extra.insertTime, extra.author, extra.secret);
            status.input_stream = null; // don't close when we return
            return ret;
View Full Code Here

        case SAME:
        case DUPLICATE:
        case OLD:
        case NO_ROOM: {
            if (!conn.getContentType().equals("rhizome-manifest/text"))
              throw new ServalDInterfaceException("unexpected HTTP Content-Type " + conn.getContentType() + " from " + conn.getURL());
            RhizomeManifest returned_manifest = RhizomeManifest.fromTextFormat(status.input_stream);
            BundleExtra extra = bundleExtraFromHeaders(conn);
            return new RhizomeInsertBundle(status.bundle_status_code, returned_manifest, extra.rowId, extra.insertTime, extra.author, extra.secret);
          }
        case INVALID:
          throw new RhizomeInvalidManifestException(status.bundle_status_message, conn.getURL());
        case FAKE:
          throw new RhizomeFakeManifestException(status.bundle_status_message, conn.getURL());
        case INCONSISTENT:
          throw new RhizomeInconsistencyException(status.bundle_status_message, conn.getURL());
        case READONLY:
          throw new RhizomeReadOnlyException(status.bundle_status_message, conn.getURL());
        }
        break;
      case TOO_BIG:
      case EVICTED:
        dumpStatus(status, System.err);
        return null;
      case WRONG_SIZE:
      case WRONG_HASH:
        dumpStatus(status, System.err);
        throw new RhizomeInconsistencyException(status.payload_status_message, conn.getURL());
      case CRYPTO_FAIL:
        dumpStatus(status, System.err);
        throw new RhizomeEncryptionException(status.payload_status_message, conn.getURL());
      }
    }
    catch (RhizomeManifestParseException e) {
      throw new ServalDInterfaceException("malformed manifest from daemon", e);
    }
    finally {
      if (status.input_stream != null)
        status.input_stream.close();
    }
View Full Code Here

  private static String headerString(HttpURLConnection conn, String header) throws ServalDInterfaceException
  {
    String str = headerStringOrNull(conn, header);
    if (str == null)
      throw new ServalDInterfaceException("missing header field: " + header);
    return str;
  }
View Full Code Here

  {
    String quoted = conn.getHeaderField(header);
    if (quoted == null)
      return null;
    if (quoted.length() == 0 || quoted.charAt(0) != '"')
      throw new ServalDInterfaceException("malformed header field: " + header + ": missing quote at start of quoted-string");
    boolean slosh = false;
    boolean end = false;
    StringBuilder b = new StringBuilder(quoted.length());
    for (int i = 1; i < quoted.length(); ++i) {
      char c = quoted.charAt(i);
      if (end)
        throw new ServalDInterfaceException("malformed header field: " + header + ": spurious character after quoted-string");
      if (c < ' ' || c > '~')
        throw new ServalDInterfaceException("malformed header field: " + header + ": invalid character in quoted-string");
      if (slosh) {
        b.append(c);
        slosh = false;
      }
      else if (c == '"')
        end = true;
      else if (c == '\\')
        slosh = true;
      else
        b.append(c);
    }
    if (!end)
      throw new ServalDInterfaceException("malformed header field: " + header + ": missing quote at end of quoted-string");
    return b.toString();
  }
View Full Code Here

TOP

Related Classes of org.servalproject.servaldna.ServalDInterfaceException

Copyright © 2018 www.massapicom. 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.