001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.net.http; 020 021import java.io.IOException; 022import java.net.URL; 023import java.util.Iterator; 024import java.util.Map; 025import java.util.Map.Entry; 026 027import lucee.commons.io.TemporaryStream; 028import lucee.commons.io.res.Resource; 029import lucee.commons.net.http.httpclient3.HTTPEngine3Impl; 030import lucee.commons.net.http.httpclient4.HTTPEngine4Impl; 031import lucee.commons.net.http.httpclient4.HeaderImpl; 032import lucee.runtime.net.proxy.ProxyData; 033import lucee.runtime.type.util.CollectionUtil; 034 035public class HTTPEngine { 036 037 private static final boolean use4=true; 038 039 /** 040 * Field <code>ACTION_POST</code> 041 */ 042 public static final short ACTION_POST=0; 043 044 /** 045 * Field <code>ACTION_GET</code> 046 */ 047 public static final short ACTION_GET=1; 048 049 /** 050 * Field <code>STATUS_OK</code> 051 */ 052 public static final int STATUS_OK=200; 053 //private static final String NO_MIMETYPE="Unable to determine MIME type of file."; 054 055 public static final int MAX_REDIRECT = 15; 056 057 058 /** 059 * Constant value for HTTP Status Code "moved Permanently 301" 060 */ 061 public static final int STATUS_REDIRECT_MOVED_PERMANENTLY=301; 062 /** 063 * Constant value for HTTP Status Code "Found 302" 064 */ 065 public static final int STATUS_REDIRECT_FOUND=302; 066 /** 067 * Constant value for HTTP Status Code "see other 303" 068 */ 069 public static final int STATUS_REDIRECT_SEE_OTHER=303; 070 071 072 073 074 public static HTTPResponse get(URL url) throws IOException { 075 if(use4) return HTTPEngine4Impl.get(url, null, null, -1,MAX_REDIRECT, null, null, null, null); 076 return HTTPEngine3Impl.get(url, null, null, -1,MAX_REDIRECT, null, null, null, null); 077 } 078 079 public static HTTPResponse post(URL url) throws IOException { 080 if(use4) return HTTPEngine4Impl.post(url, null, null, -1,MAX_REDIRECT, null, null, null, null); 081 return HTTPEngine3Impl.post(url, null, null, -1,MAX_REDIRECT, null, null, null, null,null); 082 } 083 084 public static HTTPResponse get(URL url, String username, String password, long timeout, int maxRedirect, 085 String charset, String useragent,ProxyData proxy, Header[] headers) throws IOException { 086 if(use4) return HTTPEngine4Impl.get(url, username, password, timeout, maxRedirect, charset, useragent, proxy, headers); 087 return HTTPEngine3Impl.get(url, username, password, timeout, maxRedirect, charset, useragent, proxy, headers); 088 } 089 090 public static HTTPResponse post(URL url, String username, String password, long timeout, int maxRedirect, 091 String charset, String useragent, ProxyData proxy, Map<String,String> headers, Map<String,String> params) throws IOException { 092 if(use4) return HTTPEngine4Impl.post(url, username, password, timeout, maxRedirect, charset, useragent, proxy, toHeaders(headers),params); 093 return HTTPEngine3Impl.post(url, username, password, timeout, maxRedirect, charset, useragent, proxy, toHeaders(headers),params); 094 } 095 096 public static HTTPResponse head(URL url, String username, String password, int timeout, int maxRedirect, 097 String charset, String useragent,ProxyData proxy, Header[] headers) throws IOException { 098 if(use4) return HTTPEngine4Impl.head(url, username, password, timeout, maxRedirect, charset, useragent, proxy, headers); 099 return HTTPEngine3Impl.head(url, username, password, timeout, maxRedirect, charset, useragent, proxy, headers); 100 } 101 102 public static HTTPResponse put(URL url, String username, String password, int timeout, int maxRedirect, 103 String mimetype,String charset, String useragent,ProxyData proxy, Header[] headers, Object body) throws IOException { 104 if(use4) return HTTPEngine4Impl.put(url, username, password, timeout, maxRedirect, mimetype,charset, useragent, proxy, headers,body); 105 return HTTPEngine3Impl.put(url, username, password, timeout, maxRedirect, charset, useragent, proxy, headers,body); 106 } 107 108 public static HTTPResponse delete(URL url, String username, String password, int timeout, int maxRedirect, 109 String charset, String useragent,ProxyData proxy, Header[] headers) throws IOException { 110 if(use4) return HTTPEngine4Impl.delete(url, username, password, timeout, maxRedirect, charset, useragent, proxy, headers); 111 return HTTPEngine3Impl.delete(url, username, password, timeout, maxRedirect, charset, useragent, proxy, headers); 112 } 113 114 public static Header header(String name, String value) { 115 if(use4) return HTTPEngine4Impl.header(name, value); 116 return HTTPEngine3Impl.header(name, value); 117 } 118 119 public static Entity getEmptyEntity(String contentType) { 120 if(use4) return HTTPEngine4Impl.getEmptyEntity(contentType); 121 return HTTPEngine3Impl.getEmptyEntity(contentType); 122 } 123 124 public static Entity getByteArrayEntity(byte[] barr, String contentType) { 125 if(use4) return HTTPEngine4Impl.getByteArrayEntity(barr,contentType); 126 return HTTPEngine3Impl.getByteArrayEntity(barr,contentType); 127 } 128 129 public static Entity getTemporaryStreamEntity(TemporaryStream ts, String contentType) { 130 if(use4) return HTTPEngine4Impl.getTemporaryStreamEntity(ts,contentType); 131 return HTTPEngine3Impl.getTemporaryStreamEntity(ts,contentType); 132 } 133 134 public static Entity getResourceEntity(Resource res, String contentType) { 135 if(use4) return HTTPEngine4Impl.getResourceEntity(res,contentType); 136 return HTTPEngine3Impl.getResourceEntity(res,contentType); 137 } 138 139 private static Header[] toHeaders(Map<String, String> headers) { 140 if(CollectionUtil.isEmpty(headers)) return null; 141 Header[] rtn=new Header[headers.size()]; 142 Iterator<Entry<String, String>> it = headers.entrySet().iterator(); 143 Entry<String, String> e; 144 int index=0; 145 while(it.hasNext()){ 146 e = it.next(); 147 rtn[index++]=new HeaderImpl(e.getKey(),e.getValue()); 148 } 149 return rtn; 150 } 151 152 153}