Package org.w3c.jigsaw.http

Examples of org.w3c.jigsaw.http.HTTPException


     process = makeCgiCommand (request) ;
       } catch (IOException e) {
     e.printStackTrace();
     Reply error = request.makeReply(HTTP.NOT_FOUND) ;
     error.setContent("The resource's script wasn't found.") ;
     throw new HTTPException (error) ;
       }
       return handleCGIOutput (process, request) ;
   }
View Full Code Here


        ex.getMessage() + "]");
      getServer().errlog(this, msg);
      // Throw an internal server error:
      Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR) ;
      error.setContent("CGI script is misconfigured.");
      throw new HTTPException (error) ;
  }
  // Now feed the process:
  try {
      // Send the 100 status code:
      Client client = request.getClient();
      if ( client != null )
    client.sendContinue();
      InputStream in = request.getInputStream();
      if ( in == null ) {
    // There was no input to that CCI, close process stream
    process.getOutputStream().close();
      } else {
    // Some input to feed the process with:
    (new ProcessFeeder(process, in)).start();
      }
  } catch (IOException ex) {
      // This is most probably a bad request:
      Reply error = request.makeReply(HTTP.BAD_REQUEST);
      error.setContent("The request didn't have a valid input.");
      throw new HTTPException(error);
  }
  return handleCGIOutput(process, request);
    }
View Full Code Here

  if (wrapper == null) {
      Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
      reply.setContent("Servlet Wrapper Frame not configured properly: "+
           "must be attached to a ServletWrapper.");
      throw new HTTPException(reply);
  }

  try {
      wrapper.checkServlet();
  } catch (ClassNotFoundException ex) {
      Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
      reply.setContent("The server was unable to find the "+
           "servlet class : "+ex.getMessage());
      if ( wrapper.debug )
    ex.printStackTrace();
      throw new HTTPException(reply);
  } catch (ServletException ex) {
      Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
      reply.setContent("The server was unable to initialize the "+
           "servlet : "+ex.getMessage());
      if ( wrapper.debug )
    ex.printStackTrace();
      throw new HTTPException(reply);
  }

  // Check that the servlet has been initialized properly:
  if ( ! wrapper.isInited() ) {
      Reply reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
      reply.setContent("Servlet not configured properly");
      throw new HTTPException(reply);
  }
  // Dispatch the request:
  Reply reply = createDefaultReply(request, HTTP.OK);
  reply.setContentType(MimeType.TEXT_HTML);
  try {
View Full Code Here

  updateCachedHeaders();
  // Is this resource writable ?
  if ( ! getPutableFlag() ) {
      Reply error = request.makeReply(HTTP.NOT_ALLOWED) ;
      error.setContent("Method PUT not allowed.") ;
      throw new HTTPException (error) ;
  }
  HttpEntityTag etag = getComETag();
  // no IfMatch, or no matching ETag, maybe a PUT on the image
  int cim = checkIfMatch(request, etag);
  if ((request.getIfMatch() == null) ||
      (cim == COND_FAILED) || (cim == COND_WEAK)) {
      return super.putFileResource(request);
  }
  // check all the others validator

  // Check remaining validators (checking if-none-match is lame
  // as we already require the If-Match
  if ((checkIfNoneMatch(request, etag) == COND_FAILED)
      || (checkIfModifiedSince(request) == COND_FAILED)
      || (checkIfUnmodifiedSince(request) == COND_FAILED)) {
      Reply r = request.makeReply(HTTP.PRECONDITION_FAILED);
      r.setContent("Pre-condition failed.");
      return r;
  }
  // Check the request:
  InputStream in = null;
  try {
      in = request.getInputStream();
      if ( in == null ) {
    Reply error = request.makeReply(HTTP.BAD_REQUEST) ;
    error.setContent ("<p>Request doesn't have a valid content.");
    throw new HTTPException (error) ;
      }
  } catch (IOException ex) {
      throw new ClientException(request.getClient(), ex);
  }
  // We do not support (for the time being) put with ranges:
  if ( request.hasContentRange() ) {
      Reply error = request.makeReply(HTTP.BAD_REQUEST);
      error.setContent("partial PUT not supported.");
      throw new HTTPException(error);
  }
  // Check that if some type is provided it doesn't conflict:
  if ( request.hasContentType() ) {
      MimeType rtype = request.getContentType() ;
      MimeType type  = getCommentType() ;
      if ( type == null ) {
    setValue (ATTR_CONTENT_TYPE, rtype) ;
      } else if ( rtype.match (type) < 0 ) {
    if (debug) {
        System.out.println("No match between: ["+
               rtype.toString()+"] and ["+
               type.toString()+"]");
    }
    Reply error = request.makeReply(HTTP.UNSUPPORTED_MEDIA_TYPE) ;
    error.setContent ("<p>Invalid content type: "+type.toString());
    throw new HTTPException (error) ;
      }
  }
  ImageFileResource ifresource = (ImageFileResource) fresource;
  // Write the body back to the file:
  try {
View Full Code Here

      } else if (oldlength > names.length){
    setVariants(names);
      }

  } catch (InvalidResourceException ex) {
      throw new HTTPException("invalid parent for negotiation");
  } finally {
      r_parent.unlock();
  }
  return variants ;
    }
View Full Code Here

    Reply reply = request.makeReply(HTTP.NOT_FOUND);
    reply.setContent ("<h1>Document not found</h1>"+
          "<p>The document "+request.getURL()+
          " has no acceptable variants "+
          "(probably deleted).");
    throw new HTTPException (reply);
      }
  }
  if ( variants.length < 2 ) {
      if ( variants.length == 0 ) {
    try {
        getResource().delete();
    } catch (MultipleLockException ex) {
        //will be deleted later...
    } finally {
        Reply reply = request.makeReply(HTTP.NOT_FOUND);
        reply.setContent ("<h1>Document not found</h1>"+
              "<p>The document "+request.getURL()+
              " has no acceptable variants "+
              "(probably deleted).");
        throw new HTTPException (reply);
    }
      } else {
    return variants[0] ;
      }
  }
  // Build a vector of variant negociation states, one per variants:
  Vector states = new Vector (variants.length) ;
  for (int i = 0 ; i < variants.length ; i++) {
      double qs = 1.0 ;
      try {
    FramedResource resource =
        (FramedResource) variants[i].unsafeLock() ;
    HTTPFrame itsframe =
        (HTTPFrame) resource.unsafeGetFrame(httpFrameClass);
    if (itsframe != null) {
        if ( itsframe.unsafeDefinesAttribute (ATTR_QUALITY) )
      qs = itsframe.unsafeGetQuality() ;
        if ( qs > REQUIRED_QUALITY )
      states.addElement(new VariantState (variants[i], qs)) ;
    }
      } catch (InvalidResourceException ex) {
    //FIXME
      } finally {
    variants[i].unlock();
      }
  }
  // Content-encoding negociation:
  if ( debug ) {
      printNegotiationState ("init:", states) ;
  }
  if ( negotiateContentEncoding (states, request) ) {
      // Remains a single acceptable variant:
      return ((VariantState) states.elementAt(0)).getResource() ;
  }
  if ( debug ) {
      printNegotiationState ("encoding:", states) ;
  }
  // Charset quality negociation:
  if ( negotiateCharsetQuality (states, request) ) {
      // Remains a single acceptable variant:
      return ((VariantState) states.elementAt(0)).getResource() ;
  }
  if ( debug ) {
      printNegotiationState ("charset:", states) ;
  }
  // Language quality negociation:
  if ( negotiateLanguageQuality (states, request) ) {
      // Remains a single acceptable variant:
      return ((VariantState) states.elementAt(0)).getResource() ;
  }
  if ( debug ) {
      printNegotiationState ("language:", states) ;
  }
  // Content-type negociation:
  if ( negotiateContentType (states, request) ) {
      // Remains a single acceptable variant:
      return ((VariantState) states.elementAt(0)).getResource() ;
  }
  if ( debug ) {
      printNegotiationState ("type:", states) ;
  }
  // If we reached this point, this means that multiple variants are
  // acceptable at this point. Keep the ones that have the best quality.
  if ( debug ) {
      printNegotiationState ("before Q selection:", states) ;
  }
  double qmax = REQUIRED_QUALITY ;

  for (int i=0; i< states.size() ; ) {
      VariantState state = (VariantState) states.elementAt(i) ;
      if ( state.getQ() > qmax ) {
    for (int j = i ; j > 0 ; j--)
        states.removeElementAt(0) ;
    qmax = state.getQ() ;
    i = 1 ;
      } else {
    if ( state.getQ() < qmax)
        states.removeElementAt(i) ;
    else
        i++;
      }
  }
  if ( debug )
      printNegotiationState ("After Q selection:", states) ;
  if ( qmax == REQUIRED_QUALITY ) {

      Reply reply = request.makeReply(HTTP.NOT_ACCEPTABLE) ;
       HtmlGenerator g = new HtmlGenerator("No acceptable");
       g.append("<P>The resource cannot be served according to the "
          + "headers sent</P>");
       reply.setStream (g) ;
      throw new HTTPException (reply) ;
  } else if ( states.size() == 1 ) {
      return ((VariantState) states.elementAt(0)).getResource() ;
  } else {
      // Respond with multiple choice (for the time being, there should
      // be a parameter to decide what to do.
      Reply reply = request.makeReply(HTTP.MULTIPLE_CHOICE) ;
      HtmlGenerator g = new HtmlGenerator ("Multiple choice for "+
             resource.getIdentifier()) ;
      g.append ("<ul>") ;
      for (int i = 0 ; i < states.size() ; i++) {
    VariantState state = (VariantState) states.elementAt(i) ;
    String name = null;
    ResourceReference rr = state.getResource();
    try {
        name = rr.unsafeLock().getIdentifier();
        g.append ("<li>"
            + "<a href=\"" + name + "\">" + name + "</a>"
            + " Q= " + state.getQ()) ;
    } catch (InvalidResourceException ex) {
        //FIXME
    } finally {
        rr.unlock();
    }
      }
      reply.setStream (g) ;
      reply.setHeaderValue(reply.H_VARY, getVary());
      throw new HTTPException (reply) ;
  }
    }
View Full Code Here

    Reply reply = request.makeReply(HTTP.NOT_FOUND);
    reply.setContent ("<h1>Document not found</h1>"+
          "<p>The document "+request.getURL()+
          " has no acceptable variants "+
          "(probably deleted).");
    throw new HTTPException (reply);
      }
  }
  // negotiate etag
  HttpEntityTag etag = request.getETag();
  HttpEntityTag etags[] = request.getIfMatch();
  // gather the etags
  if (etags == null && etag != null) {
      etags = new HttpEntityTag[1];
      etags[0] = etag;
  } else if (etag != null) {
      HttpEntityTag t_etags[] = new HttpEntityTag[etags.length+1];
      System.arraycopy(etags, 0, t_etags, 0, etags.length);
      t_etags[etags.length] = etag;
      etags = t_etags;
  }

  if (etags != null) {
      // yeah go for it!
      FramedResource resource;
      HttpEntityTag frametag;
      for (int i = 0 ; i < variants.length ; i++) {
    try {
        resource = (FramedResource)variants[i].unsafeLock() ;
        itsframe = (HTTPFrame)resource.getFrame(httpFrameClass);
        if (itsframe != null) {
      frametag = itsframe.getETag();
      if (frametag == null) {
          continue;
      }
      // Do we have a winner?
      try {
          for (int j=0; j<etags.length; j++)
        if(frametag.getTag().equals(etags[j].getTag()))
            return variants[i];
      } catch (NullPointerException ex) {
          // if the list of etag contains a null
          // it should never happen and the try doesn't cost
      }
        }
    } catch (InvalidResourceException ex) {
        //FIXME
    } finally {
        variants[i].unlock();
    }
      }
      // no matching variants...
      Reply reply = request.makeReply(HTTP.NOT_FOUND);
      reply.setContent ("<h1>Document not found</h1>"+
            "<p>The document "+request.getURL()+
            " has no acceptable variants "+
            "according to the ETag sent");
      throw new HTTPException (reply);
  }
  // if we are strict, don't go any further, etags
  // is the mandatory thing, otherwise PUT on the direct version
  if (getPutPolicy()) {
      Reply reply = request.makeReply(HTTP.NOT_FOUND);
      reply.setContent ("<h1>Document not found</h1>"+
            "<p>The document "+request.getURL()+
            " has no acceptable variants "+
            " for a PUT, as no ETags were sent");
      throw new HTTPException (reply);
  }
  // now filter out variants
  nb_v = variants.length;
  MimeType type = request.getContentType();
  String encodings[] = request.getContentEncoding();
  String languages[] = request.getContentLanguage();
  ResourceReference rr;

  if (type != null || encodings != null || languages != null) {
      // the request is not too bad ;)
      for (int i = 0 ; i < variants.length ; i++) {
    if (variants[i] == null)
        continue;
    rr = variants[i];
    try {
        resource = (FramedResource)rr.unsafeLock() ;
        itsframe = (HTTPFrame)resource.getFrame(httpFrameClass);
        if (itsframe == null) {
      nb_v--;
      variants[i] = null;
      continue;
        }
        // remove the non matching mime types
        if (type != null) {
      MimeType fmt = itsframe.getContentType();
      if (fmt == null || (fmt.match(type) !=
              MimeType.MATCH_SPECIFIC_SUBTYPE)) {
          nb_v--;
          variants[i] = null;
          continue;
      }
        }
        // remove the non matching languages
        if (languages != null) {
      String language = itsframe.getContentLanguage();
      nb_v--;
      variants[i] = null;
      if (language == null) {
          continue;
      }
      for (int j=0; j<languages.length; j++) {
          if (language.equals(languages[j])) {
        nb_v++;
        variants[i] = rr;
        break;
          }
      }
        }
        // remove the non matching encodings       
        if (encodings != null) {
      String encoding = itsframe.getContentEncoding();
      nb_v--;
      variants[i] = null;
      if (encoding == null) {
          continue;
      }
      for (int j=0; j<encodings.length; j++) {
          if (encoding.equals(languages[j])) {
        nb_v++;
        variants[i] = rr;
        break;
          }
      }
        }
    } catch (InvalidResourceException ex) {
        //FIXME
    } finally {
        rr.unlock();
    }
      }
      // a winner!
      if (nb_v == 1) {
    for (int i=0; i< variants.length; i++) {
        if (variants[i] != null)
      return variants[i];
    }
      }
      // no document matching
      if (nb_v <= 0 ) {
    Reply reply = request.makeReply(HTTP.NOT_FOUND);
    reply.setContent ("<h1>Document not found</h1>"+
          "<p>The document "+request.getURL()+
          " has no acceptable variants "+
          " for a PUT");
    throw new HTTPException (reply);
      }
  }
  // now we have multiple choice :(
  String name;
  Reply reply = request.makeReply(HTTP.MULTIPLE_CHOICE) ;
  HtmlGenerator g = new HtmlGenerator ("Multiple choice for "+
               resource.getIdentifier()) ;
  g.append ("<ul>") ;
  for (int i = 0 ; i < variants.length ; i++) {
      if (variants[i] != null) {
    try {
        name = variants[i].unsafeLock().getIdentifier();
        g.append ("<li>"
            + "<a href=\"" + name + "\">" +name+ "</a>");
    } catch (InvalidResourceException ex) {
        //FIXME (this should NOT happen :) )
    } finally {
        variants[i].unlock();
    }
      }
  }
  reply.setStream (g) ;
  reply.setHeaderValue(reply.H_VARY, getVary());
  throw new HTTPException (reply) ;
    }
View Full Code Here

  // This should never happen: either the negotiation succeed, or the
  // negotiate method should return an error.
  if ( selected == null ) {
      Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR) ;
      error.setContent("Error negotiating among resource's variants.");
      throw new HTTPException(error) ;
  }

  try {
      FramedResource resource = (FramedResource) selected.unsafeLock();
      Reply reply = (Reply)resource.perform(request) ;
      reply.setHeaderValue(reply.H_VARY, getVary());
      HTTPFrame itsframe =
    (HTTPFrame) resource.unsafeGetFrame(httpFrameClass);
      if (itsframe != null) {
    reply.setContentLocation(
          itsframe.getURL(request).toExternalForm()) ;
    return reply;
      }
      Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR) ;
      error.setContent("Error negotiating : "+
           "selected resource has no HTTPFrame");
      throw new HTTPException(error) ;
  } catch (InvalidResourceException ex) {
      Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR) ;
      error.setContent("Error negotiating : Invalid selected resource");
      throw new HTTPException(error) ;
  } finally {
      selected.unlock();
  }
    }
View Full Code Here

      } else {
    lr.setTarget(null);
    return true;
      }
  } catch (CvsException ex) {
      throw new HTTPException("status failed in CVS directory.");
  }
    }
View Full Code Here

  try {
      getCvsManager().refresh();
  } catch (CvsException ex) {
      Reply error =
    error(this, request, "Error while refreshing directory", ex);
      throw new HTTPException (error) ;
  }
    }
View Full Code Here

TOP

Related Classes of org.w3c.jigsaw.http.HTTPException

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.