Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for Basic Authentication workflow #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 38 additions & 4 deletions jasperreports/src/net/sf/jasperreports/engine/util/JRLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.LinkedHashMap;
Expand All @@ -42,7 +43,13 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sun.org.apache.xml.internal.security.utils.Base64;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRPropertiesUtil;
Expand All @@ -52,9 +59,6 @@
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReportsContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
* Utility class that helps load serialized objects found in various locations
Expand Down Expand Up @@ -205,7 +209,7 @@ public static Object loadObject(JasperReportsContext jasperReportsContext, URL u

try
{
is = url.openStream();
is = retrieveStreamFromUrl(url);
ois = new ContextClassLoaderObjectInputStream(jasperReportsContext, is);
obj = ois.readObject();
}
Expand Down Expand Up @@ -253,6 +257,36 @@ public static Object loadObject(JasperReportsContext jasperReportsContext, URL u
return obj;
}

private static InputStream retrieveStreamFromUrl(final URL url) throws IOException {
InputStream resultStream = null;

resultStream = retrieveStreamViaBasicAuthentication(url);
if (resultStream == null)
resultStream = retrieveStreamViaDefault(url);

return resultStream;
}

private static InputStream retrieveStreamViaDefault(final URL url) throws IOException {
return url.openStream();
}

private static InputStream retrieveStreamViaBasicAuthentication(final URL url) throws IOException {
// validate format of url
Pattern pattern = Pattern.compile("https?:\\/\\/(\\S+:\\S+)@(?:www\\.|(?!www)[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\\.[^\\s]{2,}|https?:\\/\\/(?:www\\.|(?!www))[a-zA-Z0-9]\\.[^\\s]{2,}|www\\.[a-zA-Z0-9]\\.[^\\s]{2,})$");
Matcher match = pattern.matcher(url.toString());

if (!match.matches()) return null;

String userNamePassword = match.group(1);
String authEncString = Base64.encode(userNamePassword.getBytes());
URLConnection connection = null;

connection = url.openConnection();
connection.setRequestProperty("Authorization", "Basic " + authEncString);
return connection.getInputStream();
}


/**
*
Expand Down