Hi Adrian,
Using your code I get redirected to the login page. The original problem
of Oli was that he got a server error (500) response. If you get a server
error response, try the latest platform. If the error persists, please
send me the console output of the platform (start in DEBUB level "java
-jar
org.apache.clerezza.platform.launcher.sesame-0.5-cubating-SNAPSHOT.jar -L
DEBUG").
Looking at your code it seems that you're trying to authenticate to the
platform, but it does not work. I attached code which shows an example how
to authenticate using basic authentication. I derived the example from the
code [1].
Cheers,
Manuel
[1]
http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java
On Tue, 2010-02-16 at 09:57 -0500, Adrian Hoitan wrote:
Hi Manuel,
>
> We updated to the bundle from 10.02.2010, but it seems that the problem
is still there. The code that we are using at the moment is a bit
diferent, but it should contain all the changes that you sugested. I
will paste you the code anyway, in case you can spot some other errors.
>
> HttpParams httpParams = new BasicHttpParams();
> HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
> HttpConnectionParams.setSoTimeout(httpParams, 5000);
>
> DefaultHttpClient http = new DefaultHttpClient(httpParams);
>
> UsernamePasswordCredentials c = new
UsernamePasswordCredentials(strUserName, strPassword);
> BasicCredentialsProvider cP = new BasicCredentialsProvider();
> cP.setCredentials(AuthScope.ANY, c);
> http.setCredentialsProvider(cP);
> HttpGet get;
> get = new HttpGet(strURLAddress);
> get.setHeader("Accept", "application/rdf+xml");
> httprResponse = http.execute(get);
>
> Cheers,
> Adrian.
>
> --getunik ag-------------------------------------------
> your name [email protected]
> hardturmstrasse 101 fon: +41 44 388 55 88
> ch-8005 zuerich fax: +41 44 388 55 89
>
> --latest getunik project-------------------------
> Support climate protection! http://youturn.greenpeace.ch
>
> --best of swiss web awards 2009------------------
> Gold & Silver for Connect2Earth / Bronze for WWF UK
>
> get into conversation - http://www.getunik.com
>
>
> ***********************************************************
> Think about the environment ! Do not print this mail unless necessary.
> ***********************************************************
>
> -----Original Message-----
> From: Oliver Strässer
> Sent: Mittwoch, 10. Februar 2010 13:59
> To: Adrian Hoitan
> Subject: WG: problem with Cookie Authentication Bundle / Access via Java
HTTP Call
>
>
>
> --getunik ag-------------------------------------------
> oliver straesser [email protected]
> hardturmstrasse 101 fon: +41 (0)44 388 55 88
> ch-8005 zuerich fax: +41 (0)44 388 55 89
>
> --aktuelles getunik projekt-------------------------
> Agieren Sie lokal! Geo Marketing für Ihre E-Mail Kampagne:
www.geomarketing.com
>
> --best of swiss web awards 2009------------------
> Gold & Silber für Connect2Earth / Bronze für WWF UK
>
> we make the web a better place - www.getunik.com
>
>
>
>
> *****************************************************************
> Bitte drucken Sie dieses E-Mail nur bei Bedarf aus. Die
Umwelt dankt es Ihnen.
> *****************************************************************
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected] [mailto:[email protected]]
> Gesendet: Dienstag, 9. Februar 2010 15:51
> An: [email protected]
> Betreff: Re: problem with Cookie Authentication Bundle / Access via Java
HTTP Call
>
> Hi Oli,
>
> Thanks for opening the issue. It's a bug in the platform (in
> org.apache.clerezza.triaxrs) and it is fixed now.
> The reason for this server error (500) response is that the java.net
> implementation sends a malformed ACCEPT-header. It contains "*;.2",
> which is wrong. But the Clerezza platform should only write a warning in
> the console (which it does), but at one point we missed to catch the
> exception, which lead to the server error.
> You could have also modified your code to send a proper ACCEPT-header.
> Following is your modified code that should work with the platform
> containing the bug (pre revision 908057).
>
> package test;
> import java.io.BufferedReader;
> import java.io.IOException;
> import java.io.InputStreamReader;
> import java.net.Authenticator;
> import java.net.HttpURLConnection;
> import java.net.InetAddress;
> import java.net.MalformedURLException;
> import java.net.PasswordAuthentication;
> import java.net.URL;
>
> /**
> *
> * @author osr
> */
> public class Main {
>
> /**
> * @param args the command line arguments
> */
> public static void main(String[] args) {
> System.out.println("Starting request");
>
> // TODO code application logic here
>
>
> // Access the page
> try {
> Authenticator.setDefault(new MyAuthenticator());
>
> // Create a URL for the desired page
> URL url = new
> URL("http://localhost:8080/admin/user-manager/");
> HttpURLConnection con = (HttpURLConnection)
> url.openConnection();
> con.setRequestProperty("Accept",
> "application/rdf+xml"); // set a
> proper Accept-header
> // Read all the text returned by the server
> BufferedReader in = new BufferedReader(new
> InputStreamReader(con.getInputStream()));
> String str;
> while ((str = in.readLine()) != null) {
> // str is one line of text; readLine() strips the
> newline character(s)
> System.out.println(str);
> }
> in.close();
> } catch (MalformedURLException e) {
> System.out.println("MalformedURLException");
> } catch (IOException e) {
> System.out.println("IOException: " + e.getMessage());
> }
> }
>
> public static class MyAuthenticator extends Authenticator {
> // This method is called when a password-protected URL is
> accessed
>
> @Override
> protected PasswordAuthentication getPasswordAuthentication() {
> System.out.println("getPasswordAuthentication");
> // Get information about the request
> String promptString = getRequestingPrompt();
> String hostname = getRequestingHost();
> InetAddress ipaddr = getRequestingSite();
> int port = getRequestingPort();
> // Get the username from the user...
> String username = "admin";
> // Get the password from the user...
> String password = "admin";
> // Return the information
> return new PasswordAuthentication(username,
> password.toCharArray());
> }
> }
> }
>
> Cheers,
> Manuel
>
> On Mon, 2010-02-08 at 08:36 -0500, Oliver Strässer wrote:
> > I try to connect to the Trialox and have problems with the redirecting
> to the login screen.
> > I tried the following code to connect to the Trialox system.
> >
> > if I deactivate the Clerezza - Platform Security Cookie Authentication
> Bundle, the code worked correctly and the authentication is successful.
> > if I activate the bundle my response is: Server returned HTTP response
> code: 500 for URL:
> http://localhost:8080/login?referer=%2Fmacc%2Fcontent%2Forg.greenpeace.ch&cause=1
> >
> > is that an error on my side or at the cookie bundle
> >
> > How is the correct way ?
> >
> > /*
> > * To change this template, choose Tools | Templates
> > * and open the template in the editor.
> > */
> > package test;
> >
> > import java.io.BufferedReader;
> > import java.io.IOException;
> > import java.io.InputStreamReader;
> > import java.net.Authenticator;
> > import java.net.InetAddress;
> > import java.net.MalformedURLException;
> > import java.net.PasswordAuthentication;
> > import java.net.URL;
> >
> > /**
> > *
> > * @author osr
> > */
> > public class Main {
> >
> > /**
> > * @param args the command line arguments
> > */
> > public static void main(String[] args) {
> > System.out.println("Starting request");
> >
> > // TODO code application logic here
> >
> >
> > // Access the page
> > try {
> > Authenticator.setDefault(new MyAuthenticator());
> >
> > // Create a URL for the desired page
> > URL url = new
> URL("http://localhost:8080/macc/content/org.greenpeace.ch");
> > // Read all the text returned by the server
> > BufferedReader in = new BufferedReader(new
> InputStreamReader(url.openStream()));
> > String str;
> > while ((str = in.readLine()) != null) {
> > // str is one line of text; readLine() strips the
> newline character(s)
> > System.out.println(str);
> > }
> > in.close();
> > } catch (MalformedURLException e) {
> > System.out.println("MalformedURLException");
> > } catch (IOException e) {
> > System.out.println("IOException: " + e.getMessage());
> > }
> > }
> >
> > public static class MyAuthenticator extends Authenticator {
> > // This method is called when a password-protected URL is
accessed
> >
> > @Override
> > protected PasswordAuthentication getPasswordAuthentication() {
> > System.out.println("getPasswordAuthentication");
> > // Get information about the request
> > String promptString = getRequestingPrompt();
> > String hostname = getRequestingHost();
> > InetAddress ipaddr = getRequestingSite();
> > int port = getRequestingPort();
> > // Get the username from the user...
> > String username = "admin";
> > // Get the password from the user...
> > String password = "admin";
> > // Return the information
> > return new PasswordAuthentication(username,
> password.toCharArray());
> > }
> > }
> > }
> >
> >
> >
> >
> >
> > --getunik ag-------------------------------------------
> > oliver straesser
> [email protected]<mailto:[email protected]>
> > hardturmstrasse 101 fon: +41 (0)44 388 55 88
> > ch-8005 zuerich fax: +41 (0)44 388 55 89
> >
> > --aktuelles getunik projekt-------------------------
> > Agieren Sie lokal! Geo Marketing für Ihre E-Mail Kampagne:
> www.geomarketing.com<http://www.geomarketing.com/>
> >
> > --best of swiss web awards 2009------------------
> > Gold & Silber für Connect2Earth / Bronze für WWF UK
> >
> > we make the web a better place - www.getunik.com<http://www.getunik.com/>
> >
> >
> >
> >
> > *****************************************************************
> > P Bitte drucken Sie dieses E-Mail nur bei Bedarf aus. Die Umwelt dankt
> es Ihnen.
> > *****************************************************************
> >
>
>
>
>
>package testapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
/**
*
* @author osr
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
System.out.println("Starting request");
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials("admin", "admin"));
BasicHttpContext localcontext = new BasicHttpContext();
// Generate BASIC scheme object and stick it to the local
// execution context
BasicScheme basicAuth = new BasicScheme();
localcontext.setAttribute("preemptive-auth", basicAuth);
// Add as the first request interceptor
httpclient.addRequestInterceptor(new PreemptiveAuth(), 0);
HttpGet get;
get = new HttpGet("http://localhost:8080/admin/user-manager/list-users");
get.setHeader("Accept", "application/rdf+xml");
HttpResponse httprResponse = httpclient.execute(get, localcontext);
InputStream in = httprResponse.getEntity().getContent();
String line;
StringBuffer sb = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
in.close();
}
System.out.println(sb.toString());
} catch (IOException ex) {
ex.printStackTrace();
}
}
static class PreemptiveAuth implements HttpRequestInterceptor {
public void process(
final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(
ClientContext.TARGET_AUTH_STATE);
// If no auth scheme avaialble yet, try to initialize it preemptively
if (authState.getAuthScheme() == null) {
AuthScheme authScheme = (AuthScheme) context.getAttribute(
"preemptive-auth");
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
ClientContext.CREDS_PROVIDER);
HttpHost targetHost = (HttpHost) context.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);
if (authScheme != null) {
Credentials creds = credsProvider.getCredentials(
new AuthScope(
targetHost.getHostName(),
targetHost.getPort()));
if (creds == null) {
throw new HttpException("No credentials for preemptive authentication");
}
authState.setAuthScheme(authScheme);
authState.setCredentials(creds);
}
}
}
}
}