├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── com │ └── cedricblondeau │ └── webpage2html │ ├── Configuration.java │ ├── Console.java │ ├── WebPage2Html.java │ ├── WebPage2HtmlResult.java │ ├── http │ ├── HttpCache.java │ ├── HttpRequest.java │ ├── HttpResourceFactory.java │ └── resource │ │ ├── HttpDummyResource.java │ │ ├── HttpResource.java │ │ └── HttpResponseResource.java │ └── transformers │ ├── HtmlTransformer.java │ ├── TransformerFactory.java │ └── assets │ ├── BaseTransformer.java │ ├── CssTransformer.java │ └── Transformer.java └── test └── java └── com └── cedricblondeau └── webpage2html └── transformers ├── HtmlTransformerTest.java ├── assets ├── BaseTransformerTest.java └── CssTransformerTest.java └── http └── HttpCacheUtils.java /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | build 3 | target 4 | 5 | *.iml 6 | *.class 7 | 8 | .idea 9 | .classpath 10 | .project 11 | .gradle 12 | 13 | # Vim Backup/Swap Files 14 | *~ 15 | .swp 16 | .*.swp 17 | 18 | /.settings/ 19 | /LICENSE.txt 20 | 21 | # Ignore Gradle GUI config 22 | gradle-app.setting 23 | 24 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 25 | !gradle-wrapper.jar -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Cédric Blondeau 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # webpage2html-java 2 | Generates a single HTML file for a given URL by transforming external assets (css, js, images, fonts) into inline content and by encoding them with base64 if necessary. 3 | 4 | Initially a Java port of [zTrix/webpage2html](https://github.com/zTrix/webpage2html). 5 | 6 | Java 1.7 and Android compatible. 7 | 8 | ### Known limitations 9 | - HTTP user agent is default [OkHttp](https://github.com/square/okhttp) one ("okhttp/2.4.0"). Some websites may serve different content or just block requests. [User agent spoofing](https://en.wikipedia.org/wiki/User_agent#User_agent_spoofing) may fix this but you should use this responsibly. 10 | - [Data URIs (base64) could be slower than source linking](http://www.mobify.com/blog/data-uris-are-slow-on-mobile/) 11 | 12 | ### Dependencies 13 | - [jsoup](https://github.com/jhy/jsoup) 14 | - [OkHttp](https://github.com/square/okhttp) 15 | 16 | ### Usage 17 | ```java 18 | // Build a WebPage2Html object from a java.net.URL object 19 | URL url = new URL("http://rtw.cedricblondeau.com"); // Input URL, throws MalformedURLException 20 | WebPage2Html webPage2Html = new WebPage2Html(url); 21 | 22 | // Optionally: Pass a custom configuration object 23 | Configuration configuration = new Configuration(); 24 | configuration.setUserAgent("Android"); // Custom user-agent 25 | webPage2Html.setConfiguration(configuration); 26 | 27 | // execute() method returns a WebPage2HtmlResult object 28 | WebPage2HtmlResult webPage2HtmlResult = webPage2Html.execute(); // throws IOException 29 | webPage2HtmlResult.getUrl(); // Actual URL, could be different from input URL (e.g. redirection) 30 | webPage2HtmlResult.getTitle(); // HTML document title 31 | webPage2HtmlResult.getHtml(); // Transformed HTML content 32 | ``` 33 | 34 | ### CLI usage using Gradle 35 | ``` 36 | ./gradlew run -Dexec.args="http://rtw.cedricblondeau.com out.html" 37 | ``` 38 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | group 'com.cedricblondeau' 2 | version '1.0.0' 3 | 4 | apply plugin: 'java' 5 | apply plugin: 'application' 6 | 7 | sourceCompatibility = 1.7 8 | 9 | mainClassName = "com.cedricblondeau.webpage2html.Console" 10 | run { 11 | if (System.getProperty("exec.args")) { 12 | args System.getProperty("exec.args").split() 13 | } 14 | } 15 | 16 | // create a single JAR with all dependencies 17 | task fullJar(type: Jar) { 18 | manifest { 19 | attributes 'Implementation-Title': 'Gradle Jar File Example', 20 | 'Implementation-Version': version, 21 | 'Main-Class': 'com.mkyong.DateUtils' 22 | } 23 | baseName = project.name + '-all' 24 | from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } 25 | with jar 26 | } 27 | 28 | repositories { 29 | mavenCentral() 30 | } 31 | 32 | dependencies { 33 | testCompile group: 'junit', name: 'junit', version: '4.11' 34 | testCompile group: 'commons-codec', name: 'commons-codec', version: '1.10' 35 | compile group: 'org.jsoup', name: 'jsoup', version: '1.8.3' 36 | compile group: 'com.squareup.okhttp', name: 'okhttp', version: '2.4.0' 37 | } 38 | 39 | task wrapper(type: Wrapper) { 40 | gradleVersion = '2.6' 41 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cedricblondeau/webpage2html-java/982fb81a66d49733c705412438d685231aba75af/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Aug 14 18:39:32 CEST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-bin.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'webpage2html' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/Configuration.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html; 2 | 3 | public final class Configuration { 4 | private String userAgent = null; 5 | 6 | public String getUserAgent() { 7 | return userAgent; 8 | } 9 | 10 | public Configuration setUserAgent(String userAgent) { 11 | this.userAgent = userAgent; 12 | return this; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/Console.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.IOException; 5 | import java.io.PrintWriter; 6 | import java.io.UnsupportedEncodingException; 7 | import java.net.MalformedURLException; 8 | import java.net.URL; 9 | 10 | public final class Console { 11 | 12 | public static void main(String[] args) 13 | { 14 | try { 15 | // Arguments 16 | String url = args[0]; 17 | String fileName = args[1]; 18 | 19 | // WebPage2Html 20 | WebPage2Html webPage2Html = new WebPage2Html(new URL(url)); 21 | WebPage2HtmlResult webPage2HtmlResult = webPage2Html.execute(); 22 | 23 | // Write to given file 24 | PrintWriter printWriter = new PrintWriter(fileName, "utf-8"); 25 | printWriter.print(webPage2HtmlResult.getHtml()); 26 | printWriter.close(); 27 | 28 | } catch (ArrayIndexOutOfBoundsException e) { 29 | System.out.println("Invalid arguments"); 30 | } catch (MalformedURLException e) { 31 | System.out.println("Invalid URL"); 32 | } catch (FileNotFoundException e) { 33 | System.out.println("File not found"); 34 | } catch (UnsupportedEncodingException e) { 35 | e.printStackTrace(); 36 | } catch (IOException e) { 37 | System.out.println(e.getMessage()); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/WebPage2Html.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html; 2 | 3 | import com.cedricblondeau.webpage2html.http.HttpRequest; 4 | import com.cedricblondeau.webpage2html.transformers.HtmlTransformer; 5 | import com.squareup.okhttp.Response; 6 | import com.squareup.okhttp.ResponseBody; 7 | 8 | import java.io.IOException; 9 | import java.net.URL; 10 | import java.nio.charset.Charset; 11 | 12 | public final class WebPage2Html { 13 | 14 | private Configuration configuration; 15 | private URL requestURL; 16 | 17 | /** 18 | * Create a new WebPage2Html object & Execute HTTP request with given URL 19 | * @param url 20 | */ 21 | public WebPage2Html(URL url) { 22 | requestURL = url; 23 | } 24 | 25 | /** 26 | * @param configuration 27 | */ 28 | public void setConfiguration(Configuration configuration) { 29 | this.configuration = configuration; 30 | } 31 | 32 | /** 33 | * - Execute HTTP request 34 | * - Extract content from HTTP response 35 | * - Build HtmlTransformer object 36 | * - Return a WebPage2HtmlResult object 37 | * 38 | * @return WebPage2HtmlResult 39 | */ 40 | public WebPage2HtmlResult execute() throws IOException { 41 | // If no configuration given, create a default one 42 | if (configuration == null) { 43 | configuration = new Configuration(); 44 | } 45 | 46 | // Execute request 47 | HttpRequest httpRequest = new HttpRequest(requestURL, configuration); 48 | Response httpResponse = httpRequest.execute(); 49 | URL actualURl = httpResponse.request().httpUrl().url(); 50 | 51 | // Extract content and charset 52 | ResponseBody responseBody = httpResponse.body(); 53 | String content = responseBody.string(); 54 | String charset = null; 55 | if (responseBody.contentType().charset() instanceof Charset) { 56 | charset = responseBody.contentType().charset().name(); 57 | } 58 | 59 | // Build HtmlTransformer object and transform 60 | HtmlTransformer htmlTransformer = new HtmlTransformer(content, actualURl, charset, configuration); 61 | htmlTransformer.transform(); 62 | 63 | // Build a WebPage2HtmlResult object 64 | WebPage2HtmlResult webPage2HtmlResult = new WebPage2HtmlResult(htmlTransformer); 65 | return webPage2HtmlResult; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/WebPage2HtmlResult.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html; 2 | 3 | import com.cedricblondeau.webpage2html.transformers.HtmlTransformer; 4 | 5 | import java.net.URL; 6 | 7 | public final class WebPage2HtmlResult { 8 | 9 | private URL url; 10 | private String title; 11 | private String html; 12 | 13 | public WebPage2HtmlResult(HtmlTransformer htmlTransformer) { 14 | this.url = htmlTransformer.getUrl(); 15 | this.title = htmlTransformer.getTitle(); 16 | this.html = htmlTransformer.getHtml(); 17 | } 18 | 19 | public URL getUrl() { 20 | return url; 21 | } 22 | 23 | public String getTitle() { 24 | return title; 25 | } 26 | 27 | public String getHtml() { 28 | return html; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/http/HttpCache.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.http; 2 | 3 | import java.util.Collections; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public final class HttpCache { 8 | 9 | private static final HttpCache instance = new HttpCache(); 10 | private Map cache = Collections.synchronizedMap(new HashMap()); 11 | 12 | private HttpCache() {} 13 | 14 | public static HttpCache getInstance() { 15 | return instance; 16 | } 17 | 18 | public void put(String cacheKey, Object value) { 19 | cache.put(cacheKey, value); 20 | } 21 | 22 | public boolean has(String cacheKey) { 23 | return cache.containsKey(cacheKey); 24 | } 25 | 26 | public Object get(String cacheKey) { 27 | return cache.get(cacheKey); 28 | } 29 | 30 | public void clear(String cacheKey) { 31 | cache.put(cacheKey, null); 32 | } 33 | 34 | public void clear() { 35 | cache.clear(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/http/HttpRequest.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.http; 2 | 3 | import com.cedricblondeau.webpage2html.Configuration; 4 | import com.squareup.okhttp.OkHttpClient; 5 | import com.squareup.okhttp.Request; 6 | import com.squareup.okhttp.Response; 7 | 8 | import java.io.IOException; 9 | import java.net.URL; 10 | 11 | public final class HttpRequest { 12 | 13 | private OkHttpClient client = new OkHttpClient(); 14 | private Request request; 15 | 16 | /** 17 | * @param url 18 | * @param configuration 19 | */ 20 | public HttpRequest(URL url, Configuration configuration) { 21 | Request.Builder requestBuilder = new Request.Builder().url(url); 22 | if (configuration.getUserAgent() != null && !configuration.getUserAgent().isEmpty()) { 23 | requestBuilder.addHeader("User-Agent", configuration.getUserAgent()); 24 | } 25 | request = requestBuilder.build(); 26 | } 27 | 28 | /** 29 | * @return Response 30 | */ 31 | public Response execute() throws IOException { 32 | Response response = client.newCall(request).execute(); 33 | return response; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/http/HttpResourceFactory.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.http; 2 | 3 | import com.cedricblondeau.webpage2html.Configuration; 4 | import com.cedricblondeau.webpage2html.http.resource.HttpResource; 5 | import com.cedricblondeau.webpage2html.http.resource.HttpResponseResource; 6 | import com.squareup.okhttp.ResponseBody; 7 | 8 | import java.io.IOException; 9 | import java.net.MalformedURLException; 10 | import java.net.URL; 11 | import java.util.logging.Level; 12 | import java.util.logging.Logger; 13 | 14 | public final class HttpResourceFactory { 15 | 16 | private static final Logger logger = Logger.getLogger(HttpResourceFactory.class.getName()); 17 | private Configuration configuration; 18 | 19 | public HttpResourceFactory(Configuration configuration) { 20 | this.configuration = configuration; 21 | } 22 | 23 | public HttpResource get(String resourceUrl, URL baseURL) { 24 | // Validate URL 25 | URL url; 26 | try { 27 | url = new URL(baseURL, resourceUrl); 28 | } catch (MalformedURLException e) { 29 | logger.log(Level.WARNING, e.getMessage()); 30 | return null; 31 | } 32 | 33 | // Get HttpResource 34 | try { 35 | HttpResource httpResource; 36 | if (HttpCache.getInstance().has(url.toExternalForm())) { 37 | httpResource = (HttpResource) HttpCache.getInstance().get(url.toExternalForm()); 38 | } else { 39 | HttpRequest httpRequest = new HttpRequest(url, configuration); 40 | ResponseBody responseBody = httpRequest.execute().body(); 41 | httpResource = new HttpResponseResource(responseBody, url); 42 | } 43 | return httpResource; 44 | } catch (IOException e) { 45 | logger.log(Level.SEVERE, e.getMessage()); 46 | return null; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/http/resource/HttpDummyResource.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.http.resource; 2 | 3 | import java.net.URL; 4 | 5 | public final class HttpDummyResource implements HttpResource { 6 | private URL url; 7 | private String mediaType; 8 | private String content; 9 | private byte[] data; 10 | 11 | public HttpDummyResource(URL url, String mediaType, String content, byte[] data) { 12 | this.url = url; 13 | this.mediaType = mediaType; 14 | this.content = content; 15 | this.data = data; 16 | } 17 | 18 | @Override 19 | public URL getUrl() { 20 | return url; 21 | } 22 | 23 | public void setUrl(URL url) { 24 | this.url = url; 25 | } 26 | 27 | @Override 28 | public String getMediaType() { 29 | return mediaType; 30 | } 31 | 32 | public void setMediaType(String mediaType) { 33 | this.mediaType = mediaType; 34 | } 35 | 36 | @Override 37 | public String getContent() { 38 | return content; 39 | } 40 | 41 | public void setContent(String content) { 42 | this.content = content; 43 | } 44 | 45 | @Override 46 | public byte[] getData() { 47 | return data; 48 | } 49 | 50 | public void setData(byte[] data) { 51 | this.data = data; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/http/resource/HttpResource.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.http.resource; 2 | 3 | import java.net.URL; 4 | 5 | public interface HttpResource { 6 | public URL getUrl(); 7 | public byte[] getData(); 8 | public String getContent(); 9 | public String getMediaType(); 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/http/resource/HttpResponseResource.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.http.resource; 2 | 3 | import com.squareup.okhttp.MediaType; 4 | import com.squareup.okhttp.ResponseBody; 5 | 6 | import java.io.IOException; 7 | import java.net.URL; 8 | 9 | public final class HttpResponseResource implements HttpResource { 10 | 11 | private URL url; 12 | private ResponseBody responseBody; 13 | private String mediaType; 14 | 15 | public HttpResponseResource(ResponseBody responseBody, URL url) { 16 | this.responseBody = responseBody; 17 | this.url = url; 18 | buildMediaType(); 19 | } 20 | 21 | private void buildMediaType() { 22 | MediaType contentType = responseBody.contentType(); 23 | this.mediaType = String.format("%s/%s", contentType.type(), contentType.subtype()); 24 | } 25 | 26 | @Override 27 | public URL getUrl() { 28 | return url; 29 | } 30 | 31 | @Override 32 | public byte[] getData() { 33 | try { 34 | return this.responseBody.bytes(); 35 | } catch (IOException e) { 36 | e.printStackTrace(); 37 | return null; 38 | } 39 | } 40 | 41 | @Override 42 | public String getContent() { 43 | try { 44 | return this.responseBody.string(); 45 | } catch (IOException e) { 46 | e.printStackTrace(); 47 | return null; 48 | } 49 | } 50 | 51 | @Override 52 | public String getMediaType() { 53 | return mediaType; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/transformers/HtmlTransformer.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers; 2 | 3 | import com.cedricblondeau.webpage2html.Configuration; 4 | import com.cedricblondeau.webpage2html.transformers.assets.CssTransformer; 5 | import com.cedricblondeau.webpage2html.transformers.assets.Transformer; 6 | import org.jsoup.Jsoup; 7 | import org.jsoup.nodes.Document; 8 | import org.jsoup.nodes.Element; 9 | import org.jsoup.select.Elements; 10 | 11 | import java.util.logging.Logger; 12 | import java.net.URL; 13 | 14 | public final class HtmlTransformer { 15 | 16 | private Configuration configuration; 17 | private Document document; 18 | private URL url; 19 | private String charset; 20 | private static final Logger logger = Logger.getLogger(HtmlTransformer.class.getName()); 21 | 22 | public HtmlTransformer(String content, URL url, String charset, Configuration configuration) { 23 | document = Jsoup.parse(content, url.toExternalForm()); 24 | this.url = url; 25 | this.charset = charset; 26 | this.configuration = configuration; 27 | } 28 | 29 | public void transform() { 30 | injectEncoding(); 31 | transformStyle(); 32 | transformLink(); 33 | transformScript(); 34 | transformImg(); 35 | } 36 | 37 | private void injectEncoding() { 38 | if (charset instanceof String) { 39 | boolean charsetDefinitionFound = document.head().getElementsByTag("meta").hasAttr("charset"); 40 | if (!charsetDefinitionFound) { 41 | logger.info(String.format("Injecting charset %s", charset)); 42 | document.head().append(String.format("", charset)); 43 | } 44 | } 45 | } 46 | 47 | private void transformStyle() { 48 | Elements styleElements = document.getElementsByAttribute("style"); 49 | for (Element element : styleElements) { 50 | logger.info("Transforming inline style"); 51 | CssTransformer cssTransformer = new CssTransformer(element.attr("style"), url, configuration); 52 | element.attr("style", cssTransformer.getContent()); 53 | } 54 | } 55 | 56 | private void transformLink() { 57 | Elements linkElements = document.getElementsByTag("link"); 58 | for (Element element : linkElements) { 59 | String rel = element.attr("rel"); 60 | if (!rel.isEmpty() && (rel.equals("stylesheet") || rel.equals("icon"))) { 61 | String href = element.attr("href"); 62 | if (!href.isEmpty() && !href.startsWith("data:")) { 63 | logger.info(String.format("Transforming link %s", element.attr("href"))); 64 | Transformer transformer = new TransformerFactory(configuration).get(element.attr("href"), url); 65 | if (transformer instanceof Transformer) { 66 | if (transformer instanceof CssTransformer) { 67 | element.after(String.format("", ((CssTransformer) transformer).getContent())); 68 | element.remove(); 69 | } else { 70 | element.attr("href", transformer.getBase64()); 71 | } 72 | } 73 | } 74 | } 75 | } 76 | } 77 | 78 | private void transformScript() { 79 | Elements scriptElements = document.getElementsByTag("script"); 80 | for (Element element : scriptElements) { 81 | if (element.hasAttr("src") && !element.attr("src").isEmpty() && !element.attr("src").startsWith("data:")) { 82 | logger.info(String.format("Transforming script %s", element.attr("src"))); 83 | Transformer transformer = new TransformerFactory(configuration).get(element.attr("src"), url); 84 | if (transformer instanceof Transformer) { 85 | element.attr("src", transformer.getBase64()); 86 | } 87 | } 88 | } 89 | } 90 | 91 | private void transformImg() { 92 | Elements imgElements = document.getElementsByTag("img"); 93 | for (Element element : imgElements) { 94 | if (element.hasAttr("src") && !element.attr("src").isEmpty() && !element.attr("src").startsWith("data:")) { 95 | logger.info(String.format("Transforming image %s", element.attr("src"))); 96 | Transformer transformer = new TransformerFactory(configuration).get(element.attr("src"), url); 97 | if (transformer instanceof Transformer) { 98 | element.attr("src", transformer.getBase64()); 99 | } 100 | } 101 | } 102 | } 103 | 104 | /** 105 | * @return JSoup Document 106 | */ 107 | public Document getDocument() { 108 | return document; 109 | } 110 | 111 | /** 112 | * @return JSoup Document inner HTML 113 | */ 114 | public String getHtml() { 115 | return document.html(); 116 | } 117 | 118 | /** 119 | * @return JSoup Document title 120 | */ 121 | public String getTitle() { 122 | return document.title(); 123 | } 124 | 125 | /** 126 | * @return URL 127 | */ 128 | public URL getUrl() { 129 | return url; 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/transformers/TransformerFactory.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers; 2 | 3 | import com.cedricblondeau.webpage2html.Configuration; 4 | import com.cedricblondeau.webpage2html.http.HttpResourceFactory; 5 | import com.cedricblondeau.webpage2html.http.resource.HttpResource; 6 | import com.cedricblondeau.webpage2html.transformers.assets.BaseTransformer; 7 | import com.cedricblondeau.webpage2html.transformers.assets.CssTransformer; 8 | import com.cedricblondeau.webpage2html.transformers.assets.Transformer; 9 | 10 | import java.net.URL; 11 | 12 | public final class TransformerFactory { 13 | 14 | private Configuration configuration; 15 | 16 | public TransformerFactory(Configuration configuration) { 17 | this.configuration = configuration; 18 | } 19 | 20 | /** 21 | * @param url 22 | * @param baseURL 23 | * @return ITransformer 24 | */ 25 | public Transformer get(String url, URL baseURL) { 26 | HttpResource httpResource = new HttpResourceFactory(configuration).get(url, baseURL); 27 | return this.get(httpResource); 28 | } 29 | 30 | /** 31 | * @param httpResource 32 | * @return Transformer 33 | */ 34 | public Transformer get(HttpResource httpResource) { 35 | try { 36 | switch (httpResource.getMediaType()) { 37 | case "text/css": 38 | return new CssTransformer(httpResource.getContent(), httpResource.getUrl(), configuration); 39 | default: 40 | BaseTransformer baseTransformer = new BaseTransformer(httpResource.getMediaType()); 41 | baseTransformer.setData(httpResource.getData()); 42 | return baseTransformer; 43 | } 44 | } catch (NullPointerException e) { 45 | return null; 46 | } 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/transformers/assets/BaseTransformer.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers.assets; 2 | 3 | import okio.ByteString; 4 | 5 | public class BaseTransformer implements Transformer { 6 | 7 | private byte[] data; 8 | private String mediaType; 9 | 10 | /** 11 | * @param mediaType (e.g: text/css, text/javascript, etc.) 12 | */ 13 | public BaseTransformer(String mediaType) { 14 | this.mediaType = mediaType; 15 | } 16 | 17 | /** 18 | * @param data 19 | */ 20 | public void setData(byte[] data) { 21 | this.data = data; 22 | } 23 | 24 | /** 25 | * @return String A String containing the resulting Base64 encoded characters 26 | */ 27 | @Override 28 | public String getBase64() { 29 | return String.format("data:%s;base64,%s", mediaType, ByteString.of(data).base64()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/transformers/assets/CssTransformer.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers.assets; 2 | 3 | import com.cedricblondeau.webpage2html.Configuration; 4 | import com.cedricblondeau.webpage2html.http.HttpResourceFactory; 5 | import com.cedricblondeau.webpage2html.http.resource.HttpResource; 6 | 7 | import java.net.URL; 8 | import java.util.logging.Logger; 9 | import java.util.regex.Matcher; 10 | import java.util.regex.Pattern; 11 | 12 | /** 13 | * TODO: 14 | * - Recursively transform CSS files (@import) 15 | */ 16 | public final class CssTransformer extends BaseTransformer implements Transformer { 17 | 18 | private String content; 19 | private URL baseURL; 20 | private Configuration configuration; 21 | private static final Logger logger = Logger.getLogger(CssTransformer.class.getName()); 22 | 23 | /** 24 | * @param content CSS content 25 | * @param baseURL CSS file base URL 26 | */ 27 | public CssTransformer(String content, URL baseURL, Configuration configuration) { 28 | super("text/css"); 29 | this.content = content; 30 | this.baseURL = baseURL; 31 | this.configuration = configuration; 32 | transform(); 33 | } 34 | 35 | /** 36 | * Replace each URL between url('*') with a base64 value 37 | */ 38 | protected void transform() { 39 | Matcher m = Pattern.compile("url\\((.*?)\\)").matcher(content); 40 | while(m.find()) { 41 | String foundURL = m.group(1); 42 | foundURL = foundURL.replace("\"", "").replace("\'", ""); 43 | if (!foundURL.startsWith("data:")) { 44 | logger.info(String.format("%s - Transforming %s", baseURL, foundURL)); 45 | HttpResource httpResource = new HttpResourceFactory(configuration).get(foundURL, baseURL); 46 | if (httpResource instanceof HttpResource) { 47 | BaseTransformer transformer = new BaseTransformer(httpResource.getMediaType()); 48 | transformer.setData(httpResource.getData()); 49 | content = content.replace(foundURL, transformer.getBase64()); 50 | } 51 | } 52 | } 53 | } 54 | 55 | /** 56 | * @return String Transformed CSS content 57 | */ 58 | public String getContent() { 59 | return content; 60 | } 61 | 62 | /** 63 | * @return String A String containing the resulting Base64 encoded characters 64 | */ 65 | @Override 66 | public String getBase64() { 67 | setData(this.content.getBytes()); 68 | return super.getBase64(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/cedricblondeau/webpage2html/transformers/assets/Transformer.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers.assets; 2 | 3 | public interface Transformer { 4 | public String getBase64(); 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/com/cedricblondeau/webpage2html/transformers/HtmlTransformerTest.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers; 2 | 3 | import com.cedricblondeau.webpage2html.Configuration; 4 | import com.cedricblondeau.webpage2html.transformers.http.HttpCacheUtils; 5 | import junit.framework.TestCase; 6 | import org.jsoup.nodes.Element; 7 | 8 | import java.net.MalformedURLException; 9 | import java.net.URL; 10 | 11 | public class HtmlTransformerTest extends TestCase { 12 | 13 | private HtmlTransformer getHtmlTransformer(String html) { 14 | try { 15 | URL url = new URL("http://www.cedricblondeau.com/"); 16 | return new HtmlTransformer(html, url, "UTF-8", new Configuration()); 17 | } catch (MalformedURLException e) { 18 | e.printStackTrace(); 19 | return null; 20 | } 21 | } 22 | 23 | public void testShouldInjectMetaCharsetIfNotPresent() { 24 | String html = "Héllo world!"; 25 | HtmlTransformer htmlTransformer = getHtmlTransformer(html); 26 | htmlTransformer.transform(); 27 | assertTrue(htmlTransformer.getDocument().head().getElementsByTag("meta").hasAttr("charset")); 28 | } 29 | 30 | public void testShouldTransformStyleAttributeValue() { 31 | String base64 = "WW91IGRpZG4ndCBzYXkgdGhlIG1hZ2ljIHdvcmQh"; 32 | HttpCacheUtils.cacheMockResourceFromBase64("http://www.cedricblondeau.com/img/test.png", "image/png", base64); 33 | String html = "
"; 34 | HtmlTransformer htmlTransformer = getHtmlTransformer(html); 35 | htmlTransformer.transform(); 36 | Element element = htmlTransformer.getDocument().getElementById("myDiv"); 37 | assertTrue(element.attr("style").contains(String.format("data:image/png;base64,%s", base64))); 38 | } 39 | 40 | public void testShouldTransformLinkStylesheet() { 41 | String css = "body { background: red; }"; 42 | HttpCacheUtils.cacheMockResourceFromSource("http://www.cedricblondeau.com/css/test.css", "text/css", css); 43 | String html = ""; 44 | HtmlTransformer htmlTransformer = getHtmlTransformer(html); 45 | htmlTransformer.transform(); 46 | Element element = htmlTransformer.getDocument().getElementsByTag("style").first(); 47 | assertEquals(css, element.html()); 48 | } 49 | 50 | public void testShouldNotTransformLinkCanonical() { 51 | String html = ""; 52 | HtmlTransformer htmlTransformer = getHtmlTransformer(html); 53 | htmlTransformer.transform(); 54 | Element element = htmlTransformer.getDocument().getElementsByTag("link").first(); 55 | assertEquals(html, element.outerHtml()); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/com/cedricblondeau/webpage2html/transformers/assets/BaseTransformerTest.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers.assets; 2 | 3 | import junit.framework.TestCase; 4 | 5 | import org.apache.commons.codec.binary.Base64; 6 | 7 | public class BaseTransformerTest extends TestCase { 8 | 9 | public void testConvertToBase64() { 10 | String mediaType = "image/png"; 11 | BaseTransformer baseTransformer = new BaseTransformer(mediaType); 12 | String imageBase64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="; 13 | byte[] imageData = Base64.decodeBase64(imageBase64); 14 | baseTransformer.setData(imageData); 15 | assertEquals("Resource must be transformed to Base64", String.format("data:%s;base64,%s", mediaType, imageBase64), baseTransformer.getBase64()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/test/java/com/cedricblondeau/webpage2html/transformers/assets/CssTransformerTest.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers.assets; 2 | 3 | import com.cedricblondeau.webpage2html.Configuration; 4 | import com.cedricblondeau.webpage2html.transformers.http.HttpCacheUtils; 5 | import junit.framework.TestCase; 6 | 7 | import java.net.MalformedURLException; 8 | import java.net.URL; 9 | 10 | public class CssTransformerTest extends TestCase { 11 | 12 | URL cssUrl; 13 | 14 | @Override 15 | protected void setUp() throws Exception { 16 | super.setUp(); 17 | 18 | // CSS file URL 19 | try { 20 | cssUrl = new URL("http://www.cedricblondeau.com/css/test.css"); 21 | } catch (MalformedURLException e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | 26 | public void testWithData() { 27 | String css = "background: url(data:image/png,base64:ABC!);"; 28 | CssTransformer cssTransformer = new CssTransformer(css, cssUrl, new Configuration()); 29 | assertEquals("Data should not be transformed", css, cssTransformer.getContent()); 30 | } 31 | 32 | public void testWithPngImage() { 33 | String base64 = "WW91IGRpZG4ndCBzYXkgdGhlIG1hZ2ljIHdvcmQh"; 34 | String mediaType = "image/png"; 35 | HttpCacheUtils.cacheMockResourceFromBase64("http://www.cedricblondeau.com/img/test.png", mediaType, base64); 36 | String css = "background: url('/img/test.png');"; 37 | CssTransformer cssTransformer = new CssTransformer(css, cssUrl, new Configuration()); 38 | String expectedCss = String.format("background: url(\'data:%s;base64,%s\');", mediaType, base64); 39 | assertEquals("Image URL must be transformed to Base64", expectedCss, cssTransformer.getContent()); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/com/cedricblondeau/webpage2html/transformers/http/HttpCacheUtils.java: -------------------------------------------------------------------------------- 1 | package com.cedricblondeau.webpage2html.transformers.http; 2 | 3 | import com.cedricblondeau.webpage2html.http.HttpCache; 4 | import com.cedricblondeau.webpage2html.http.resource.HttpDummyResource; 5 | import com.cedricblondeau.webpage2html.http.resource.HttpResource; 6 | 7 | import java.net.MalformedURLException; 8 | import java.net.URL; 9 | import org.apache.commons.codec.binary.Base64; 10 | 11 | public class HttpCacheUtils { 12 | 13 | public static void cacheMockResourceFromBase64(String url, String mediaType, String base64) { 14 | try { 15 | HttpResource resource = new HttpDummyResource( 16 | new URL(url), 17 | mediaType, 18 | null, 19 | Base64.decodeBase64(base64) 20 | ); 21 | HttpCache.getInstance().put(resource.getUrl().toExternalForm(), resource); 22 | } catch (MalformedURLException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | public static void cacheMockResourceFromSource(String url, String mediaType, String content) { 28 | try { 29 | HttpResource resource = new HttpDummyResource( 30 | new URL(url), 31 | mediaType, 32 | content, 33 | content.getBytes() 34 | ); 35 | HttpCache.getInstance().put(resource.getUrl().toExternalForm(), resource); 36 | } catch (MalformedURLException e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | 41 | } 42 | --------------------------------------------------------------------------------