mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 00:15:44 +01:00
return error stream, if input stream could not be returned
This commit is contained in:
@@ -33,11 +33,13 @@ package sonia.scm.net.ahc;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.common.collect.HashMultimap;
|
|
||||||
import com.google.common.collect.LinkedHashMultimap;
|
import com.google.common.collect.LinkedHashMultimap;
|
||||||
import com.google.common.collect.Multimap;
|
import com.google.common.collect.Multimap;
|
||||||
import com.google.common.io.ByteSource;
|
import com.google.common.io.ByteSource;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
//~--- JDK imports ------------------------------------------------------------
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -132,6 +134,14 @@ public class DefaultAdvancedHttpResponse extends AdvancedHttpResponse
|
|||||||
private static class URLConnectionByteSource extends ByteSource
|
private static class URLConnectionByteSource extends ByteSource
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* the logger for URLConnectionByteSource
|
||||||
|
*/
|
||||||
|
private static final Logger logger =
|
||||||
|
LoggerFactory.getLogger(URLConnectionByteSource.class);
|
||||||
|
|
||||||
|
//~--- constructors -------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new {@link URLConnectionByteSource}.
|
* Constructs a new {@link URLConnectionByteSource}.
|
||||||
*
|
*
|
||||||
@@ -146,17 +156,35 @@ public class DefaultAdvancedHttpResponse extends AdvancedHttpResponse
|
|||||||
//~--- methods ------------------------------------------------------------
|
//~--- methods ------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens the http connection.
|
* Opens the input stream of http connection, if an error occurs during
|
||||||
|
* opening the method will return the error stream instead.
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @return http connection
|
* @return input or error stream of http connection
|
||||||
*
|
*
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public InputStream openStream() throws IOException
|
public InputStream openStream() throws IOException
|
||||||
{
|
{
|
||||||
return connection.getInputStream();
|
InputStream stream;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
stream = connection.getInputStream();
|
||||||
|
}
|
||||||
|
catch (IOException ex)
|
||||||
|
{
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
{
|
||||||
|
logger.debug(
|
||||||
|
"could not open input stream, open error stream instead", ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
stream = connection.getErrorStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
return stream;
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- fields -------------------------------------------------------------
|
//~--- fields -------------------------------------------------------------
|
||||||
|
|||||||
@@ -92,6 +92,28 @@ public class DefaultAdvancedHttpResponseTest
|
|||||||
assertEquals("test", content.asCharSource(Charsets.UTF_8).read());
|
assertEquals("test", content.asCharSource(Charsets.UTF_8).read());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testContentAsByteSourceWithFailedRequest() throws IOException
|
||||||
|
{
|
||||||
|
ByteArrayInputStream bais =
|
||||||
|
new ByteArrayInputStream("test".getBytes(Charsets.UTF_8));
|
||||||
|
|
||||||
|
when(connection.getInputStream()).thenThrow(IOException.class);
|
||||||
|
when(connection.getErrorStream()).thenReturn(bais);
|
||||||
|
|
||||||
|
AdvancedHttpResponse response = new DefaultAdvancedHttpResponse(connection,
|
||||||
|
404, "NOT FOUND");
|
||||||
|
ByteSource content = response.contentAsByteSource();
|
||||||
|
|
||||||
|
assertEquals("test", content.asCharSource(Charsets.UTF_8).read());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user