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.runtime.util; 020 021import java.io.IOException; 022import java.io.UnsupportedEncodingException; 023import java.net.MalformedURLException; 024import java.net.URI; 025import java.net.URISyntaxException; 026import java.net.URL; 027 028import lucee.commons.net.URLDecoder; 029import lucee.commons.net.URLEncoder; 030import lucee.commons.net.http.HTTPEngine; 031import lucee.commons.net.http.HTTPResponse; 032import lucee.commons.net.http.Header; 033import lucee.runtime.net.proxy.ProxyDataImpl; 034 035public class HTTPUtilImpl implements HTTPUtil { 036 037 038 039 private static HTTPUtil instance=new HTTPUtilImpl(); 040 041 private HTTPUtilImpl(){} 042 043 public static HTTPUtil getInstance() { 044 return instance; 045 } 046 047 /** 048 * @see lucee.runtime.util.HTTPUtil#decode(java.lang.String, java.lang.String) 049 */ 050 public String decode(String str, String charset)throws UnsupportedEncodingException { 051 return URLDecoder.decode(str, charset,false); 052 } 053 054 /** 055 * @see lucee.runtime.util.HTTPUtil#delete(java.net.URL, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, lucee.commons.net.http.Header[]) 056 */ 057 public HTTPResponse delete(URL url, String username, String password, 058 int timeout, String charset, String useragent, String proxyserver, 059 int proxyport, String proxyuser, String proxypassword, 060 Header[] headers) throws IOException { 061 return HTTPEngine.delete(url, username, password, timeout,HTTPEngine.MAX_REDIRECT, charset, useragent, ProxyDataImpl.getInstance(proxyserver, proxyport, proxyuser, proxypassword), headers); 062 } 063 064 /** 065 * @param str 066 * @param charset 067 * @return 068 * @throws UnsupportedEncodingException 069 */ 070 public String encode(String str, String charset)throws UnsupportedEncodingException { 071 return URLEncoder.encode(str, charset); 072 } 073 074 /** 075 * @see lucee.runtime.util.HTTPUtil#head(java.net.URL, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, lucee.commons.net.http.Header[]) 076 */ 077 public HTTPResponse head(URL url, String username, String password, 078 int timeout, String charset, String useragent, String proxyserver, 079 int proxyport, String proxyuser, String proxypassword, 080 Header[] headers) throws IOException { 081 return HTTPEngine.head(url, username, password, timeout,HTTPEngine.MAX_REDIRECT, charset, useragent, ProxyDataImpl.getInstance(proxyserver, proxyport, proxyuser, proxypassword), headers); 082 } 083 084 /** 085 * @see lucee.runtime.util.HTTPUtil#get(java.net.URL, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, lucee.commons.net.http.Header[]) 086 */ 087 public HTTPResponse get(URL url, String username, String password, 088 int timeout, String charset, String useragent, String proxyserver, 089 int proxyport, String proxyuser, String proxypassword, 090 Header[] headers) throws IOException { 091 return HTTPEngine.get(url, username, password, timeout,HTTPEngine.MAX_REDIRECT, charset, useragent, ProxyDataImpl.getInstance(proxyserver, proxyport, proxyuser, proxypassword), headers); 092 } 093 094 /** 095 * @see lucee.runtime.util.HTTPUtil#put(java.net.URL, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String, int, java.lang.String, java.lang.String, lucee.commons.net.http.Header[], java.lang.Object) 096 */ 097 public HTTPResponse put(URL url, String username, String password, 098 int timeout, String charset, String useragent, String proxyserver, 099 int proxyport, String proxyuser, String proxypassword, 100 Header[] headers, Object body) throws IOException { 101 return put(url, username, proxypassword, timeout, null, charset, useragent, proxyserver, proxyport, proxyuser, proxypassword, headers, body); 102 } 103 104 // FUTURE add to interface 105 public HTTPResponse put(URL url, String username, String password, 106 int timeout, String mimetype, String charset, String useragent, String proxyserver, 107 int proxyport, String proxyuser, String proxypassword, 108 Header[] headers, Object body) throws IOException { 109 return HTTPEngine.put(url, username, password, timeout,HTTPEngine.MAX_REDIRECT, mimetype, charset, useragent, ProxyDataImpl.getInstance(proxyserver, proxyport, proxyuser, proxypassword), headers, body); 110 } 111 112 @Override 113 public URL toURL(String strUrl, int port) throws MalformedURLException { 114 return toURL(strUrl, port, true); 115 } 116 117 public URL toURL(String strUrl, int port, boolean encodeIfNecessary) throws MalformedURLException { 118 return lucee.commons.net.HTTPUtil.toURL(strUrl, port,encodeIfNecessary); 119 } 120 121 122 123 /** 124 * @see lucee.commons.net.HTTPUtil#toURL(java.lang.String) 125 */ 126 public URL toURL(String strUrl) throws MalformedURLException { 127 return lucee.commons.net.HTTPUtil.toURL(strUrl,true); 128 } 129 130 public URI toURI(String strUrl) throws URISyntaxException { 131 return lucee.commons.net.HTTPUtil.toURI(strUrl); 132 } 133 134 public URI toURI(String strUrl, int port) throws URISyntaxException { 135 return lucee.commons.net.HTTPUtil.toURI(strUrl,port); 136 } 137 138 139}