Package java.net

Examples of java.net.ProtocolException


    */
   @Override
   public synchronized OutputStream getOutputStream() throws IOException
   {
      if (connected)
         throw new ProtocolException("Already connected!");

      if (!doOutput)
         throw new ProtocolException("Output not enabled! (use setDoOutput(true))");
      if (!method_set)
         method = "POST";
      else if (method.equals("HEAD") || method.equals("GET") || method.equals("TRACE"))
         throw new ProtocolException("Method " + method + " does not support output!");

      if (getRequestProperty("Content-type") == null)
         setRequestProperty("Content-type", "application/x-www-form-urlencoded");

      if (output_stream == null)
View Full Code Here


         // get cookie name and value first

         end = set_cookie.indexOf('=', beg);
         if (end == -1)
            throw new ProtocolException("Bad Set-Cookie header: " + set_cookie + "\nNo '=' found "
               + "for token starting at " + "position " + beg);
         curr.name = set_cookie.substring(beg, end).trim();

         beg = Util.skipSpace(buf, end + 1);
         int comma = set_cookie.indexOf(',', beg);
         int semic = set_cookie.indexOf(';', beg);
         if (comma == -1 && semic == -1)
            end = len;
         else if (comma == -1)
            end = semic;
         else if (semic == -1)
            end = comma;
         else
         {
            if (comma > semic)
               end = semic;
            else
            {
               // try to handle broken servers which put commas
               // into cookie values
               int eq = set_cookie.indexOf('=', comma);
               if (eq > 0 && eq < semic)
                  end = set_cookie.lastIndexOf(',', eq);
               else
                  end = semic;
            }
         }
         curr.value = set_cookie.substring(beg, end).trim();

         beg = end;

         // now parse attributes

         boolean legal = true;
         parts : while (true) // parse all parts
         {
            if (beg >= len || buf[beg] == ',')
               break;

            // skip empty fields
            if (buf[beg] == ';')
            {
               beg = Util.skipSpace(buf, beg + 1);
               continue;
            }

            // first check for secure, as this is the only one w/o a '='
            if ((beg + 6 <= len) && set_cookie.regionMatches(true, beg, "secure", 0, 6))
            {
               curr.secure = true;
               beg += 6;

               beg = Util.skipSpace(buf, beg);
               if (beg < len && buf[beg] == ';') // consume ";"
                  beg = Util.skipSpace(buf, beg + 1);
               else if (beg < len && buf[beg] != ',')
                  throw new ProtocolException("Bad Set-Cookie header: " + set_cookie + "\nExpected "
                     + "';' or ',' at position " + beg);

               continue;
            }

            // alright, must now be of the form x=y
            end = set_cookie.indexOf('=', beg);
            if (end == -1)
               throw new ProtocolException("Bad Set-Cookie header: " + set_cookie + "\nNo '=' found "
                  + "for token starting at " + "position " + beg);

            String name = set_cookie.substring(beg, end).trim();
            beg = Util.skipSpace(buf, end + 1);
View Full Code Here

         {
            age = Integer.parseInt(value);
         }
         catch (NumberFormatException nfe)
         {
            throw new ProtocolException("Bad Set-Cookie header: " + set_cookie + "\nMax-Age '" + value
               + "' not a number");
         }
         cookie.expires = new Date(System.currentTimeMillis() + age * 1000L);
      }
      else if (name.equalsIgnoreCase("domain"))
View Full Code Here

      {
         cookies = Util.parseHeader(set_cookie);
      }
      catch (ParseException pe)
      {
         throw new ProtocolException(pe.getMessage());
      }

      Cookie cookie_arr[] = new Cookie[cookies.size()];
      int cidx = 0;
      for (int idx = 0; idx < cookie_arr.length; idx++)
      {
         HttpHeaderElement c_elem = (HttpHeaderElement)cookies.elementAt(idx);

         // set NAME and VALUE

         if (c_elem.getValue() == null)
            throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nMissing value " + "for cookie '"
               + c_elem.getName() + "'");
         Cookie2 curr = new Cookie2(req);
         curr.name = c_elem.getName();
         curr.value = c_elem.getValue();

         // set all params

         NVPair[] params = c_elem.getParams();
         boolean discard_set = false, secure_set = false;
         for (int idx2 = 0; idx2 < params.length; idx2++)
         {
            String name = params[idx2].getName().toLowerCase();

            // check for required value parts
            if ((name.equals("version") || name.equals("max-age") || name.equals("domain") || name.equals("path")
               || name.equals("comment") || name.equals("commenturl"))
               && params[idx2].getValue() == null)
            {
               throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nMissing value " + "for "
                  + params[idx2].getName() + " attribute in cookie '" + c_elem.getName() + "'");
            }

            if (name.equals("version")) // Version
            {
               if (curr.version != -1)
                  continue;
               try
               {
                  curr.version = Integer.parseInt(params[idx2].getValue());
               }
               catch (NumberFormatException nfe)
               {
                  throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nVersion '"
                     + params[idx2].getValue() + "' not a number");
               }
            }
            else if (name.equals("path")) // Path
            {
               if (curr.path_set)
                  continue;
               curr.path = params[idx2].getValue();
               curr.path_set = true;
            }
            else if (name.equals("domain")) // Domain
            {
               if (curr.domain_set)
                  continue;
               String d = params[idx2].getValue().toLowerCase();

               // add leading dot if not present and if domain is
               // not the full host name
               if (d.charAt(0) != '.' && !d.equals(curr.domain))
                  curr.domain = "." + d;
               else
                  curr.domain = d;
               curr.domain_set = true;
            }
            else if (name.equals("max-age")) // Max-Age
            {
               if (curr.expires != null)
                  continue;
               int age;
               try
               {
                  age = Integer.parseInt(params[idx2].getValue());
               }
               catch (NumberFormatException nfe)
               {
                  throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nMax-Age '"
                     + params[idx2].getValue() + "' not a number");
               }
               curr.expires = new Date(System.currentTimeMillis() + age * 1000L);
            }
            else if (name.equals("port")) // Port
            {
               if (curr.port_set)
                  continue;

               if (params[idx2].getValue() == null)
               {
                  curr.port_list = new int[1];
                  curr.port_list[0] = req.getConnection().getPort();
                  curr.port_set = true;
                  continue;
               }

               curr.port_list_str = params[idx2].getValue();
               StringTokenizer tok = new StringTokenizer(params[idx2].getValue(), ",");
               curr.port_list = new int[tok.countTokens()];
               for (int idx3 = 0; idx3 < curr.port_list.length; idx3++)
               {
                  String port = tok.nextToken().trim();
                  try
                  {
                     curr.port_list[idx3] = Integer.parseInt(port);
                  }
                  catch (NumberFormatException nfe)
                  {
                     throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nPort '" + port
                        + "' not a number");
                  }
               }
               curr.port_set = true;
            }
            else if (name.equals("discard")) // Domain
            {
               if (discard_set)
                  continue;
               curr.discard = true;
               discard_set = true;
            }
            else if (name.equals("secure")) // Secure
            {
               if (secure_set)
                  continue;
               curr.secure = true;
               secure_set = true;
            }
            else if (name.equals("comment")) // Comment
            {
               if (curr.comment != null)
                  continue;
               try
               {
                  curr.comment = new String(params[idx2].getValue().getBytes("8859_1"), "UTF8");
               }
               catch (UnsupportedEncodingException usee)
               {
                  throw new Error(usee.toString()); /* shouldn't happen */
               }
            }
            else if (name.equals("commenturl")) // CommentURL
            {
               if (curr.comment_url != null)
                  continue;
               try
               {
                  curr.comment_url = new URI(params[idx2].getValue());
               }
               catch (ParseException pe)
               {
                  throw new ProtocolException("Bad Set-Cookie2 header: " + set_cookie + "\nCommentURL '"
                     + params[idx2].getValue() + "' not a valid URL");
               }
            }
            // ignore unknown element
         }
View Full Code Here

    {
            inputStream = new ByteArrayInputStream(getDirectoryListing());
    }

  if (doOutput)
    throw new ProtocolException
      ("file: protocol does not support output on directories");
      }
   
    connected = true;
  }
View Full Code Here

   */
  public InputStream getInputStream()
    throws IOException
  {
    if (!doInput)
      throw new ProtocolException("Can't open InputStream if doInput is false");
   
    if (!connected)
      connect();
   
    return inputStream;
View Full Code Here

   */
  public OutputStream getOutputStream()
    throws IOException
  {
    if (!doOutput)
      throw new
  ProtocolException("Can't open OutputStream if doOutput is false");

    if (!connected)
      connect();
   
View Full Code Here

   * @see #getRequestMethod()
   * @see #method
   */
  public void setRequestMethod(String method) throws ProtocolException {
    if (connected) {
            throw new ProtocolException(Msg.getString("K0037"));
        }
    for (int i = 0; i < methodTokens.length; i++) {
      if (methodTokens[i].equals(method)) {
        // if there is a supported method that matches the desired
        // method, then set the current method and return
        this.method = methodTokens[i];
        return;
      }
    }
    // if none matches, then throw ProtocolException
    throw new ProtocolException();
  }
View Full Code Here

    }

    @Override
    public InputStream getInputStream() throws IOException {
        if (!doInput) {
            throw new ProtocolException(Msg.getString("K008d"));
        }

        doRequest();

        /*
 
View Full Code Here

    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        if (!doOutput) {
            throw new ProtocolException(Msg.getString("K008e"));
        }

        // you can't write after you read
        if (sentRequest) {
            throw new ProtocolException(Msg.getString("K0090"));
        }

        if (os != null) {
            return os;
        }

        // they are requesting a stream to write to. This implies a POST method
        if (method == GET) {
            method = POST;
        }

        // If the request method is neither PUT or POST, then you're not writing
        if (method != PUT && method != POST) {
            throw new ProtocolException(Msg.getString("K008f", method));
        }

        int limit = -1;
        String contentLength = reqHeader.get("Content-Length");
        if (contentLength != null) {
View Full Code Here

TOP

Related Classes of java.net.ProtocolException

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.