Package java.net

Examples of java.net.MalformedURLException


    /**
     * Create from a URI.
     */
    public ClientCHK(FreenetURI uri) throws MalformedURLException {
        if(!uri.getKeyType().equals("CHK"))
            throw new MalformedURLException("Not CHK");
        routingKey = uri.getRoutingKey();
        cryptoKey = uri.getCryptoKey();
        byte[] extra = uri.getExtra();
        if((extra == null) || (extra.length < 5))
            throw new MalformedURLException("No extra bytes in CHK - maybe a 0.5 key?");
        // byte 0 is reserved, for now
        cryptoAlgorithm = extra[1];
    if(!(cryptoAlgorithm == Key.ALGO_AES_PCFB_256_SHA256 || cryptoAlgorithm == Key.ALGO_AES_CTR_256_SHA256))
      throw new MalformedURLException("Invalid crypto algorithm");
        controlDocument = (extra[2] & 0x02) != 0;
        compressionAlgorithm = (short)(((extra[3] & 0xff) << 8) + (extra[4] & 0xff));
        hashCode = Fields.hashCode(routingKey) ^ Fields.hashCode(cryptoKey) ^ compressionAlgorithm;
    }
View Full Code Here


    byte[] extra = new byte[EXTRA_LENGTH];
    dis.readFully(extra);
    // byte 0 is reserved, for now
        cryptoAlgorithm = extra[1];
    if(!(cryptoAlgorithm == Key.ALGO_AES_PCFB_256_SHA256 || cryptoAlgorithm == Key.ALGO_AES_CTR_256_SHA256))
      throw new MalformedURLException("Invalid crypto algorithm");
        compressionAlgorithm = (short)(((extra[3] & 0xff) << 8) + (extra[4] & 0xff));
        controlDocument = (extra[2] & 0x02) != 0;
    routingKey = new byte[NodeCHK.KEY_LENGTH];
    dis.readFully(routingKey);
    cryptoKey = new byte[CRYPTO_KEY_LENGTH];
View Full Code Here

    private static final long serialVersionUID = 1L;
    public final DSAPrivateKey privKey;
 
  public static InsertableUSK createInsertable(FreenetURI uri, boolean persistent) throws MalformedURLException {
    if(!uri.getKeyType().equalsIgnoreCase("USK"))
      throw new MalformedURLException();
    InsertableClientSSK ssk =
      InsertableClientSSK.create(uri.setKeyType("SSK"));
    return new InsertableUSK(ssk.docName, ssk.pubKeyHash, ssk.cryptoKey, ssk.privKey, uri.getSuggestedEdition(), ssk.cryptoAlgorithm);
  }
View Full Code Here

  }
 
  InsertableUSK(String docName, byte[] pubKeyHash, byte[] cryptoKey, DSAPrivateKey key, long suggestedEdition, byte cryptoAlgorithm) throws MalformedURLException {
    super(pubKeyHash, cryptoKey, docName, suggestedEdition, cryptoAlgorithm);
    if(cryptoKey.length != ClientSSK.CRYPTO_KEY_LENGTH)
      throw new MalformedURLException("Decryption key wrong length: "+cryptoKey.length+" should be "+ClientSSK.CRYPTO_KEY_LENGTH);
    this.privKey = key;
  }
View Full Code Here

          if (errors != null) {
            tempURI = tempURI.setMetaString(null);
            errors.add("URI did contain meta strings, removed it for you");
            if (breakOnErrors) break Outer;
          } else {
            throw new MalformedURLException("URIs with meta strings not supported");
          }
        }
        break;
      }
      case SSKFORUSK: {
        if (tempURI.isUSK()) {
          if (errors != null) {
            tempURI = tempURI.sskForUSK();
            errors.add("URI was an USK, converted it to SSK for you");
            if (breakOnErrors) break Outer;
          } else {
            throw new MalformedURLException("USK not supported, use underlying SSK instead.");
          }
        }
        break;
      }
      default : Logger.error(URISanitizer.class, "Illegal Option, how can this happen?");
View Full Code Here

   * @throws MalformedURLException If the string could not be parsed.
   */
  public FreenetURI(String URI, boolean noTrim) throws MalformedURLException {
//    this.uniqueHashCode = super.hashCode();
    if(URI == null)
      throw new MalformedURLException("No URI specified");

    if(!noTrim)
      URI = URI.trim();
   
    // Strip ?max-size, ?type etc.
    // Un-encoded ?'s are illegal.
    int x = URI.indexOf('?');
    if(x > -1)
      URI = URI.substring(0, x);
     
    if(URI.indexOf('@') < 0 || URI.indexOf('/') < 0)
      // Encoded URL?
      try {
        URI = URLDecoder.decode(URI, false);
      } catch(URLEncodedFormatException e) {
        throw new MalformedURLException("Invalid URI: no @ or /, or @ or / is escaped but there are invalid escapes");
      }

    URI = URI_PREFIX.matcher(URI).replaceFirst("");

    // decode keyType
    int atchar = URI.indexOf('@');
    if(atchar == -1)
      throw new MalformedURLException("There is no @ in that URI! (" + URI + ')');

    String _keyType = URI.substring(0, atchar).toUpperCase();
    URI = URI.substring(atchar + 1);

    boolean validKeyType = false;
    for(int i = 0; i < VALID_KEY_TYPES.length; i++) {
      if (_keyType.equals(VALID_KEY_TYPES[i])) {
        validKeyType = true;
        _keyType = VALID_KEY_TYPES[i];
        break;
      }
    }
    keyType = _keyType;
    if(!validKeyType)
      throw new MalformedURLException("Invalid key type: " + keyType);

    boolean isSSK = "SSK".equals(keyType);
    boolean isUSK = "USK".equals(keyType);
    boolean isKSK = "KSK".equals(keyType);

    // decode metaString
    ArrayList<String> sv = null;
    int slash2;
    sv = new ArrayList<String>();
    if (isKSK) URI = "/" + URI; // ensure that KSK docNames are decoded
    while ((slash2 = URI.lastIndexOf('/')) != -1) {
      String s;
      try {
        s = URLDecoder.decode(URI.substring(slash2 + 1 /* "/".length() */), true);
      } catch(URLEncodedFormatException e) {
        throw (MalformedURLException)new MalformedURLException(e.toString()).initCause(e);
      }
      if(s != null)
        sv.add(s);
      URI = URI.substring(0, slash2);
    }

    // sv is *backwards*
    // this makes for more efficient handling

    // SSK@ = create a random SSK
    if(sv.isEmpty() && (isUSK || isKSK))
      throw new MalformedURLException("No docname for " + keyType);
   
    if((isSSK || isUSK || isKSK) && !sv.isEmpty()) {

      docName = sv.remove(sv.size() - 1);
      if(isUSK) {
        if(sv.isEmpty())
          throw new MalformedURLException("No suggested edition number for USK");
        try {
          suggestedEdition = Long.parseLong(sv.remove(sv.size() - 1));
        } catch(NumberFormatException e) {
          throw (MalformedURLException)new MalformedURLException("Invalid suggested edition: " + e).initCause(e);
        }
      } else
        suggestedEdition = -1;
    } else {
      // docName not necessary, nor is it supported, for CHKs.
      docName = null;
      suggestedEdition = -1;
    }

    if(!sv.isEmpty()) {
      metaStr = new String[sv.size()];
      for(int i = 0; i < metaStr.length; i++) {
        metaStr[i] = sv.get(metaStr.length - 1 - i).intern();
        if(metaStr[i] == null)
          throw new NullPointerException();
      }
    } else
      metaStr = null;

    if(isKSK) {
      routingKey = extra = cryptoKey = null;
      return;
    }

    // strip 'file extensions' from CHKs
    // added by aum (david@rebirthing.co.nz)
    if("CHK".equals(keyType)) {
      int idx = URI.lastIndexOf('.');
      if(idx != -1)
        URI = URI.substring(0, idx);
    }

    // URI now contains: routingKey[,cryptoKey][,metaInfo]
    StringTokenizer st = new StringTokenizer(URI, ",");
    try {
      if(st.hasMoreTokens()) {
        routingKey = Base64.decode(st.nextToken());
        if(routingKey.length != 32 && keyType.equals("CHK"))
          throw new MalformedURLException("Bad URI: Routing key should be 32 bytes long");
      } else {
        routingKey = cryptoKey = extra = null;
        return;
      }
      if(!st.hasMoreTokens()) {
        cryptoKey = extra = null;
        return;
      }

      // Can be cryptokey or name-value pair.
      String t = st.nextToken();
      cryptoKey = Base64.decode(t);
      if(cryptoKey.length != 32)
        throw new MalformedURLException("Bad URI: Routing key should be 32 bytes long");
      if(!st.hasMoreTokens()) {
        extra = null;
        return;
      }
      extra = Base64.decode(st.nextToken());

    } catch(IllegalBase64Exception e) {
      throw new MalformedURLException("Invalid Base64 quantity: " + e);
    }
    if (logDEBUG) Logger.debug(this, "Created from parse: "+toString()+" from "+URI, new Exception("debug"));
  }
View Full Code Here

    else if(type == SSK)
      keyType = "SSK";
    else if(type == KSK)
      keyType = "KSK";
    else
      throw new MalformedURLException("Unrecognized type " + type);
    byte[] routingKey = null;
    byte[] cryptoKey = null;
    byte[] extra = null;
    if((type == CHK) || (type == SSK)) {
      // routingKey is a hash, so is exactly 32 bytes
View Full Code Here

    DataOutputStream ndos = new DataOutputStream(baos);
    writeFullBinaryKey(ndos);
    ndos.close();
    byte[] data = baos.toByteArray();
    if(data.length > Short.MAX_VALUE)
      throw new MalformedURLException("Full key too long: " + data.length + " - " + this);
    dos.writeShort((short) data.length);
    if(logMINOR)
      Logger.minor(this, "Written " + data.length + " bytes");
    dos.write(data);
  }
View Full Code Here

    else if(keyType.equals("SSK"))
      dos.writeByte(SSK);
    else if(keyType.equals("KSK"))
      dos.writeByte(KSK);
    else if(keyType.equals("USK"))
      throw new MalformedURLException("Cannot write USKs as binary keys");
    else
      throw new MalformedURLException("Cannot write key of type " + keyType + " - do not know how");
    if(!keyType.equals("KSK")) {
      if(routingKey.length != 32)
        throw new MalformedURLException("Routing key must be of length 32");
      dos.write(routingKey);
      if(cryptoKey.length != 32)
        throw new MalformedURLException("Crypto key must be of length 32");
      dos.write(cryptoKey);
      if(keyType.equals("CHK") && (extra.length != ClientCHK.EXTRA_LENGTH))
        throw new MalformedURLException("Wrong number of extra bytes for CHK");
      if(keyType.equals("SSK") && (extra.length != ClientSSK.EXTRA_LENGTH))
        throw new MalformedURLException("Wrong number of extra bytes for SSK");
      dos.write(extra);
    }
    if(!keyType.equals("CHK"))
      dos.writeUTF(docName);
    if(metaStr != null) {
View Full Code Here

  public MediaType(String mediaType) throws NullPointerException, MalformedURLException {
    if (mediaType == null) {
      throw new NullPointerException("contentType must not be null");
    }
    if(!DefaultMIMETypes.isPlausibleMIMEType(mediaType))
        throw new MalformedURLException("Doesn't look like a MIME type");
    int slash = mediaType.indexOf('/');
    if (slash == -1) {
      throw new MalformedURLException("mediaType does not contain ā€˜/ā€™!");
    }
    type = mediaType.substring(0, slash);
    int semicolon = mediaType.indexOf(';');
    if (semicolon == -1) {
      subtype = mediaType.substring(slash + 1);
      return;
    }
    subtype = mediaType.substring(slash + 1, semicolon).trim();
    String[] parameters = mediaType.substring(semicolon + 1).split(";");
    for (String parameter : parameters) {
      int equals = parameter.indexOf('=');
      if (equals == -1) {
        throw new MalformedURLException(String.format("Illegal parameter: ā€œ%sā€", parameter));
      }
      String name = parameter.substring(0, equals).trim().toLowerCase();
      String value = parameter.substring(equals + 1).trim();
      if(value.startsWith("\"") && value.endsWith("\""))
          value = value.substring(1, value.length()-1).trim();
View Full Code Here

TOP

Related Classes of java.net.MalformedURLException

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.