Package railo.runtime.exp

Examples of railo.runtime.exp.HTTPException


     
     
      if(!e.done){
        httpMethod.abort();
        if(throwonerror)
          throw new HTTPException("408 Request Time-out","a timeout occurred in tag http",408,"Time-out",null);
        setRequestTimeout(cfhttp)
        return;
        //throw new ApplicationException("timeout"); 
      }
    }
    httpMethod=e.httpMethod;
/////////////////////////////////////////// EXECUTE /////////////////////////////////////////////////
    int status = httpMethod.getStatusCode();
   
    String responseCharset=charset;
  // Write Response Scope
    //String rawHeader=httpMethod.getStatusLine().toString();
      String mimetype=null;
      String contentEncoding=null;
     
    // status code
      cfhttp.set(STATUSCODE,((httpMethod.getStatusCode()+" "+httpMethod.getStatusText()).trim()));
      cfhttp.set(STATUS_CODE,new Double(httpMethod.getStatusCode()));
      cfhttp.set(STATUS_TEXT,(httpMethod.getStatusText()));
      cfhttp.set(HTTP_VERSION,(httpMethod.getStatusLine().getHttpVersion()));
     
    //responseHeader
      Header[] headers = httpMethod.getResponseHeaders();
      StringBuffer raw=new StringBuffer(httpMethod.getStatusLine().toString()+" ");
      Struct responseHeader = new StructImpl();
      Array setCookie = new ArrayImpl();
     
          for(int i=0;i<headers.length;i++) {
            Header header=headers[i];
            //print.ln(header);
           
            raw.append(header+" ");
            if(header.getName().equalsIgnoreCase("Set-Cookie"))
              setCookie.append(header.getValue());
            else {
                //print.ln(header.getName()+"-"+header.getValue());
              Object value=responseHeader.get(KeyImpl.getInstance(header.getName()),null);
              if(value==null) responseHeader.set(KeyImpl.getInstance(header.getName()),header.getValue());
              else {
                  Array arr=null;
                  if(value instanceof Array) {
                      arr=(Array) value;
                  }
                  else {
                      arr=new ArrayImpl();
                      responseHeader.set(KeyImpl.getInstance(header.getName()),arr);
                      arr.appendEL(value);
                  }
                  arr.appendEL(header.getValue());
              }
            }
           
            // Content-Type
            if(header.getName().equalsIgnoreCase("Content-Type")) {
              mimetype=header.getValue();
              if(mimetype==null)mimetype=NO_MIMETYPE;
            }
           
            // Content-Encoding
            if(header.getName().equalsIgnoreCase("Content-Encoding")) {
              contentEncoding=header.getValue();
            }
           
          }
          cfhttp.set(RESPONSEHEADER,responseHeader);
          responseHeader.set(STATUS_CODE,new Double(httpMethod.getStatusCode()));
          responseHeader.set(EXPLANATION,(httpMethod.getStatusText()));
          if(setCookie.size()>0)responseHeader.set(SET_COOKIE,setCookie);
         
      // is text
          boolean isText=
            mimetype == null || 
            mimetype == NO_MIMETYPE || HTTPUtil.isTextMimeType(mimetype);
           
         
        
          cfhttp.set(TEXT,Caster.toBoolean(isText));
         
      // mimetype charset
          //boolean responseProvideCharset=false;
          if(!StringUtil.isEmpty(mimetype)){
            if(isText) {
              String[] types=HTTPUtil.splitMimeTypeAndCharset(mimetype,null);
              if(types!=null) {
                if(types[0]!=null)cfhttp.set(MIME_TYPE,types[0]);
                if(types[1]!=null)cfhttp.set(CHARSET,types[1]);
              }
                 
            }
            else cfhttp.set(MIME_TYPE,mimetype);
          }
          else cfhttp.set(MIME_TYPE,NO_MIMETYPE);

      // File
          Resource file=null;
         
          if(strFile!=null && strPath!=null) {
              file=ResourceUtil.toResourceNotExisting(pageContext, strPath).getRealResource(strFile);
          }
          else if(strFile!=null) {
              file=ResourceUtil.toResourceNotExisting(pageContext, strFile);
          }
          else if(strPath!=null) {
              file=ResourceUtil.toResourceNotExisting(pageContext, strPath);
              //Resource dir = file.getParentResource();
              if(file.isDirectory()){
                file=file.getRealResource(httpMethod.getURI().getName());
              }
             
          }
          if(file!=null)pageContext.getConfig().getSecurityManager().checkFileLocation(file);
         
         
          // filecontent
          //try {
          //print.ln(">> "+responseCharset);

        InputStream is=null;
        if(isText && getAsBinary!=GET_AS_BINARY_YES) {
          String str;
                try {
                  is = httpMethod.getResponseBodyAsStream();
                    if(is!=null &&isGzipEncoded(contentEncoding))
                      is = new GZIPInputStream(is);
                         
                    try {
                      str = is==null?"":IOUtil.toString(is,responseCharset);
                    }
                    catch (UnsupportedEncodingException uee) {
                      str = IOUtil.toString(is,(Charset)null);
                    }
                }
                catch (IOException ioe) {
                  throw Caster.toPageException(ioe);
                }
                finally {
                  IOUtil.closeEL(is);
                }
                   
                if(str==null)str="";
            if(resolveurl){
              //URI uri = httpMethod.getURI();
              if(e.redirectURL!=null)url=e.redirectURL.toExternalForm();
              str=new URLResolver().transform(str,new URL(url),false);
            }
            cfhttp.set(FILE_CONTENT,str);
            try {
              if(file!=null){
                IOUtil.write(file,str,pageContext.getConfig().getWebCharset(),false);
                    }
                }
            catch (IOException e1) {}
           
            if(name!=null) {
                    Query qry = CSVParser.toQuery( str, delimiter, textqualifier, columns, firstrowasheaders  );
                    pageContext.setVariable(name,qry);
            }
        }
        // Binary
        else {
          byte[] barr=null;
            if(isGzipEncoded(contentEncoding)){
              is = new GZIPInputStream(httpMethod.getResponseBodyAsStream());
              try {
                barr = IOUtil.toBytes(is);
          }
              catch (IOException t) {
                throw Caster.toPageException(t);
          }
          finally{
            IOUtil.closeEL(is);
          }
            }
            else {
              try {
                barr = httpMethod.getResponseBody();
          }
              catch (IOException t) {
                throw Caster.toPageException(t);
          }
            }
             
            cfhttp.set(FILE_CONTENT,barr);
           
            if(file!=null) {
              try {
                if(barr!=null)IOUtil.copy(new ByteArrayInputStream(barr),file,true);
              }
              catch (IOException ioe) {
                    throw Caster.toPageException(ioe);
              }
            }  
        }
         
      // header   
          cfhttp.set(HEADER,raw.toString());
        
          if(status!=STATUS_OK){
              cfhttp.setEL(ERROR_DETAIL,httpMethod.getStatusCode()+" "+httpMethod.getStatusText());
              if(throwonerror){
                int code=httpMethod.getStatusCode();
                String text=httpMethod.getStatusText();
                String msg=code+" "+text;
                throw new HTTPException(msg,null,code,text,null);
              }
          }
    }
    finally {
      releaseConnection(httpMethod);
View Full Code Here


    }
   
    if(method.getStatusCode()!=200){int code=method.getStatusCode();
      String text=method.getStatusText();
      String msg=code+" "+text;
      throw new HTTPException(msg,null,code,text,method.getURL());
    }
    //Object o =
      CreateObject.doWebService(null, strUrl+"?wsdl");
     
  }
View Full Code Here

     
     
      if(!e.done){
        req.abort();
        if(throwonerror)
          throw new HTTPException("408 Request Time-out","a timeout occurred in tag http",408,"Time-out",rsp.getURL());
        setRequestTimeout(cfhttp)
        return;
        //throw new ApplicationException("timeout"); 
      }
    }
   
/////////////////////////////////////////// EXECUTE /////////////////////////////////////////////////
    Charset responseCharset=CharsetUtil.toCharset(rsp.getCharset());
  // Write Response Scope
    //String rawHeader=httpMethod.getStatusLine().toString();
      String mimetype=null;
      String contentEncoding=null;
     
    // status code
      cfhttp.set(STATUSCODE,((rsp.getStatusCode()+" "+rsp.getStatusText()).trim()));
      cfhttp.set(STATUS_CODE,new Double(rsp.getStatusCode()));
      cfhttp.set(STATUS_TEXT,(rsp.getStatusText()));
      cfhttp.set(HTTP_VERSION,(rsp.getProtocolVersion()));
     
    //responseHeader
      railo.commons.net.http.Header[] headers = rsp.getAllHeaders();
      StringBuffer raw=new StringBuffer(rsp.getStatusLine()+" ");
      Struct responseHeader = new StructImpl();
      Struct cookie;
      Array setCookie = new ArrayImpl();
      Query cookies=new QueryImpl(new String[]{"name","value","path","domain","expires","secure","httpOnly"},0,"cookies");
     
          for(int i=0;i<headers.length;i++) {
            railo.commons.net.http.Header header=headers[i];
            //print.ln(header);
           
            raw.append(header.toString()+" ");
            if(header.getName().equalsIgnoreCase("Set-Cookie")) {
              setCookie.append(header.getValue());
              parseCookie(cookies,header.getValue());
            }
            else {
                //print.ln(header.getName()+"-"+header.getValue());
              Object value=responseHeader.get(KeyImpl.getInstance(header.getName()),null);
              if(value==null) responseHeader.set(KeyImpl.getInstance(header.getName()),header.getValue());
              else {
                  Array arr=null;
                  if(value instanceof Array) {
                      arr=(Array) value;
                  }
                  else {
                      arr=new ArrayImpl();
                      responseHeader.set(KeyImpl.getInstance(header.getName()),arr);
                      arr.appendEL(value);
                  }
                  arr.appendEL(header.getValue());
              }
            }
           
            // Content-Type
            if(header.getName().equalsIgnoreCase("Content-Type")) {
              mimetype=header.getValue();
              if(mimetype==null)mimetype=NO_MIMETYPE;
            }
           
            // Content-Encoding
            if(header.getName().equalsIgnoreCase("Content-Encoding")) {
              contentEncoding=header.getValue();
            }
           
          }
          cfhttp.set(RESPONSEHEADER,responseHeader);
          cfhttp.set(KeyConstants._cookies,cookies);
          responseHeader.set(STATUS_CODE,new Double(rsp.getStatusCode()));
          responseHeader.set(EXPLANATION,(rsp.getStatusText()));
          if(setCookie.size()>0)responseHeader.set(SET_COOKIE,setCookie);
         
      // is text
          boolean isText=
            mimetype == null || 
            mimetype == NO_MIMETYPE || HTTPUtil.isTextMimeType(mimetype);
           
        // is multipart
          boolean isMultipart= MultiPartResponseUtils.isMultipart(mimetype);       
        
          cfhttp.set(KeyConstants._text,Caster.toBoolean(isText));
         
      // mimetype charset
          //boolean responseProvideCharset=false;
          if(!StringUtil.isEmpty(mimetype,true)){
            if(isText) {
              String[] types=HTTPUtil.splitMimeTypeAndCharset(mimetype,null);
              if(types[0]!=null)cfhttp.set(KeyConstants._mimetype,types[0]);
              if(types[1]!=null)cfhttp.set(CHARSET,types[1]);
                 
            }
            else cfhttp.set(KeyConstants._mimetype,mimetype);
          }
          else cfhttp.set(KeyConstants._mimetype,NO_MIMETYPE);

      // File
          Resource file=null;
         
          if(strFile!=null && strPath!=null) {
              file=ResourceUtil.toResourceNotExisting(pageContext, strPath).getRealResource(strFile);
          }
          else if(strFile!=null) {
              file=ResourceUtil.toResourceNotExisting(pageContext, strFile);
          }
          else if(strPath!=null) {
              file=ResourceUtil.toResourceNotExisting(pageContext, strPath);
              //Resource dir = file.getParentResource();
              if(file.isDirectory()){
                file=file.getRealResource(req.getURI().getPath());// TODO was getName() ->http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/URI.html#getName()
              }
             
          }
          if(file!=null)pageContext.getConfig().getSecurityManager().checkFileLocation(file);
         
         
          // filecontent
          InputStream is=null;
        if(isText && getAsBinary!=GET_AS_BINARY_YES) {
          String str;
                try {
                 
                  // read content
                  if(method!=METHOD_HEAD) {
                    is = rsp.getContentAsStream();
                      if(is!=null &&isGzipEncoded(contentEncoding))
                        is = rsp.getStatusCode()!=200? new CachingGZIPInputStream(is):new GZIPInputStream(is);
                  }   
                    try {
                      try{
                      str = is==null?"":IOUtil.toString(is,responseCharset);
                      }
                      catch(EOFException eof){
                        if(is instanceof CachingGZIPInputStream) {
                          str = IOUtil.toString(is=((CachingGZIPInputStream)is).getRawData(),responseCharset);
                        }
                        else throw eof;
                      }
                    }
                    catch (UnsupportedEncodingException uee) {
                      str = IOUtil.toString(is,(Charset)null);
                    }
                }
                catch (IOException ioe) {
                  throw Caster.toPageException(ioe);
                }
                finally {
                  IOUtil.closeEL(is);
                }
                   
                if(str==null)str="";
            if(resolveurl){
              //if(e.redirectURL!=null)url=e.redirectURL.toExternalForm();
              str=new URLResolver().transform(str,e.response.getTargetURL(),false);
            }
            cfhttp.set(FILE_CONTENT,str);
            try {
              if(file!=null){
                IOUtil.write(file,str,pageContext.getConfig().getWebCharset(),false);
                    }
                }
            catch (IOException e1) {}

            if(name!=null) {
                    Query qry = CSVParser.toQuery( str, delimiter, textqualifier, columns, firstrowasheaders  );
                    pageContext.setVariable(name,qry);
            }
        }
        // Binary
        else {
          byte[] barr=null;
            if(isGzipEncoded(contentEncoding)){
              if(method!=METHOD_HEAD) {
                is=rsp.getContentAsStream();
                is = rsp.getStatusCode()!=200?new CachingGZIPInputStream(is) :new GZIPInputStream(is);
              }
             
              try {
                try{
                  barr = is==null?new byte[0]: IOUtil.toBytes(is);
                }
                catch(EOFException eof){
                  if(is instanceof CachingGZIPInputStream)
                    barr = IOUtil.toBytes(((CachingGZIPInputStream)is).getRawData());
                  else throw eof;
                }
          }
              catch (IOException t) {
                throw Caster.toPageException(t);
          }
          finally{
            IOUtil.closeEL(is);
          }
            }
            else {
              try {
                if(method!=METHOD_HEAD) barr = rsp.getContentAsByteArray();
                else barr=new byte[0];
          }
              catch (IOException t) {
                throw Caster.toPageException(t);
          }
            }
            //IF Multipart response get file content and parse parts
            if(barr!=null) {
            if(isMultipart) {
              cfhttp.set(FILE_CONTENT,MultiPartResponseUtils.getParts(barr,mimetype));
            } else {
              cfhttp.set(FILE_CONTENT,barr);
            }
            }
            else
            cfhttp.set(FILE_CONTENT,"");
           
           
            if(file!=null) {
              try {
                if(barr!=null)IOUtil.copy(new ByteArrayInputStream(barr),file,true);
              }
              catch (IOException ioe) {
                    throw Caster.toPageException(ioe);
              }
            }  
        }
         
      // header   
          cfhttp.set(KeyConstants._header,raw.toString());
          if(!isStatusOK(rsp.getStatusCode())){
            String msg=rsp.getStatusCode()+" "+rsp.getStatusText();
              cfhttp.setEL(ERROR_DETAIL,msg);
              if(throwonerror){
                throw new HTTPException(msg,null,rsp.getStatusCode(),rsp.getStatusText(),rsp.getURL());
              }
          }
    }
    finally {
      //rsp.release();
View Full Code Here

TOP

Related Classes of railo.runtime.exp.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.