[DOXIA-478] snippet macro fails if online: add a parameter to skip network error.
git-svn-id: https://svn.apache.org/repos/asf/maven/doxia/doxia/trunk@1382987 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetMacro.java b/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetMacro.java
index b2177dc..9071dca 100644
--- a/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetMacro.java
+++ b/doxia-core/src/main/java/org/apache/maven/doxia/macro/snippet/SnippetMacro.java
@@ -24,41 +24,56 @@
import org.apache.maven.doxia.macro.MacroExecutionException;
import org.apache.maven.doxia.macro.MacroRequest;
import org.apache.maven.doxia.sink.Sink;
+import org.apache.maven.doxia.sink.SinkEventAttributeSet;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.util.StringUtils;
-import java.io.IOException;
import java.io.File;
+import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
-import org.apache.maven.doxia.sink.SinkEventAttributeSet;
/**
* A macro that prints out the content of a file or a URL.
*
* @version $Id$
*/
-@Component( role = Macro.class, hint = "snippet" )
+@Component (role = Macro.class, hint = "snippet")
public class SnippetMacro
extends AbstractMacro
{
- /** Holds the cache. */
+ /**
+ * Holds the cache.
+ */
private static Map<String, String> cache = new HashMap<String, String>();
private static final int HOUR = 60;
- /** One hour default cache. */
+ /**
+ * One hour default cache.
+ */
private long timeout = HOUR * HOUR * 1000;
- /** Holds the time cache. */
+ /**
+ * Holds the time cache.
+ */
private static Map<String, Long> timeCached = new HashMap<String, Long>();
- /** Debug. */
+ /**
+ * Debug.
+ */
private boolean debug = false;
- /** {@inheritDoc} */
+ /**
+ * in case of Exception during snippet download error will ignored and empty content returned.
+ */
+ private boolean ignoreDownloadError;
+
+ /**
+ * {@inheritDoc}
+ */
public void execute( Sink sink, MacroRequest request )
throws MacroExecutionException
{
@@ -68,6 +83,20 @@
String fileParam = (String) request.getParameter( "file" );
+ String debugParam = (String) request.getParameter( "debug" );
+
+ if ( debugParam != null )
+ {
+ this.debug = Boolean.parseBoolean( debugParam );
+ }
+
+ String ignoreDownloadErrorParam = (String) request.getParameter( "ignoreDownloadError" );
+
+ if ( ignoreDownloadErrorParam != null )
+ {
+ this.ignoreDownloadError = Boolean.parseBoolean( ignoreDownloadErrorParam );
+ }
+
boolean verbatim = true;
String verbatimParam = (String) request.getParameter( "verbatim" );
@@ -142,7 +171,7 @@
* Return a snippet of the given url.
*
* @param url The URL to parse.
- * @param id The id of the snippet.
+ * @param id The id of the snippet.
* @return The snippet.
* @throws IOException if something goes wrong.
*/
@@ -164,14 +193,21 @@
}
else
{
- result = new SnippetReader( url ).readSnippet( id );
-
- cacheSnippet( url, id, result.toString() );
-
- if ( debug )
+ try
{
- result.append( "(Fetched from url, cache content " ).append( cache ).append( ")" );
+ result = new SnippetReader( url ).readSnippet( id );
+ cacheSnippet( url, id, result.toString() );
+ if ( debug )
+ {
+ result.append( "(Fetched from url, cache content " ).append( cache ).append( ")" );
+ }
}
+ catch ( IOException e )
+ {
+ result = new StringBuffer( "Error during retrieving content skip as ignoreDownloadError activated." );
+ }
+
+
}
return result;
@@ -181,7 +217,7 @@
* Return a snippet from the cache.
*
* @param url The URL to parse.
- * @param id The id of the snippet.
+ * @param id The id of the snippet.
* @return The snippet.
*/
private String getCachedSnippet( URL url, String id )
@@ -198,7 +234,7 @@
* the current timeout.
*
* @param url The URL to parse.
- * @param id The id of the snippet.
+ * @param id The id of the snippet.
* @return True if timeout exceeded.
*/
boolean isCacheTimedout( URL url, String id )
@@ -210,7 +246,7 @@
* Return the time the snippet has been cached.
*
* @param url The URL to parse.
- * @param id The id of the snippet.
+ * @param id The id of the snippet.
* @return The cache time.
*/
long timeInCache( URL url, String id )
@@ -222,7 +258,7 @@
* Return the absolute value of when the snippet has been cached.
*
* @param url The URL to parse.
- * @param id The id of the snippet.
+ * @param id The id of the snippet.
* @return The cache time.
*/
long getTimeCached( URL url, String id )
@@ -236,7 +272,7 @@
* Removes the snippet from the cache.
*
* @param url The URL to parse.
- * @param id The id of the snippet.
+ * @param id The id of the snippet.
*/
private void removeFromCache( URL url, String id )
{
@@ -251,9 +287,9 @@
* Return a global identifier for the snippet.
*
* @param url The URL to parse.
- * @param id The id of the snippet.
+ * @param id The id of the snippet.
* @return An identifier, concatenated url and id,
- * or just url.toString() if id is empty or null.
+ * or just url.toString() if id is empty or null.
*/
private String globalSnippetId( URL url, String id )
{
@@ -268,8 +304,8 @@
/**
* Puts the given snippet into the cache.
*
- * @param url The URL to parse.
- * @param id The id of the snippet.
+ * @param url The URL to parse.
+ * @param id The id of the snippet.
* @param content The content of the snippet.
*/
public void cacheSnippet( URL url, String id, String content )
diff --git a/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java b/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java
index b1fa4e6..1263fa7 100644
--- a/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java
+++ b/doxia-core/src/test/java/org/apache/maven/doxia/macro/snippet/SnippetMacroTest.java
@@ -19,26 +19,24 @@
* under the License.
*/
-import java.io.File;
-
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-
import org.apache.maven.doxia.macro.MacroExecutionException;
import org.apache.maven.doxia.macro.MacroRequest;
import org.apache.maven.doxia.sink.SinkEventElement;
import org.apache.maven.doxia.sink.SinkEventTestingSink;
-
import org.codehaus.plexus.PlexusTestCase;
+import java.io.File;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
/**
* Test snippet macro.
*
* @author ltheussl
*/
public class SnippetMacroTest
- extends PlexusTestCase
+ extends PlexusTestCase
{
/**
* Test of execute method, of class SnippetMacro.
@@ -46,7 +44,7 @@
* @throws MacroExecutionException if a macro fails during testing.
*/
public void testExecute()
- throws MacroExecutionException
+ throws MacroExecutionException
{
File basedir = new File( getBasedir() );
Map<String, Object> macroParameters = new HashMap<String, Object>();
@@ -114,4 +112,29 @@
assertTrue( snippet.contains( "second snippet" ) );
assertFalse( snippet.contains( "conclusion" ) );
}
+
+ public void testIgnoreDownloadError()
+ throws Exception
+ {
+ Map<String, Object> macroParameters = new HashMap<String, Object>();
+ macroParameters.put( "debug", "true" );
+ macroParameters.put( "ignoreDownloadError", "true" );
+
+ macroParameters.put( "url", "http://foo.bar.com/wine.txt" );
+
+ File basedir = new File( getBasedir() );
+
+ SinkEventTestingSink sink = new SinkEventTestingSink();
+
+ MacroRequest request = new MacroRequest( macroParameters, basedir );
+ SnippetMacro macro = new SnippetMacro();
+ macro.execute( sink, request );
+ Iterator<SinkEventElement> it = sink.getEventList().iterator();
+ assertEquals( "verbatim", ( it.next() ).getName() );
+ SinkEventElement event = it.next();
+ assertEquals( "text", event.getName() );
+ String snippet = (String) event.getArgs()[0];
+ assertTrue( snippet.contains( "Error during retrieving content" ) );
+
+ }
}