Announcement

Collapse
No announcement yet.

Problem with developing a java client

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with developing a java client

    Hi there,
    my target is to develop a java client, which has access to the open xchange
    http-api and lists the available calendars. I tried to access it with the following login-method:

    public User loginUser(String name, String password, String redirect) {
    BufferedReader reader = null;
    try {
    URL url = new URL("http://localhost:8084/ajax/login?action=login&name=" + name + "&password=" + password);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    cookie += conn.getHeaderField("Set-Cookie");
    if (cookie.contains("; path=/")) {
    cookie = cookie.substring(0, cookie.indexOf("; path=/"));
    }

    System.out.println("cookie: " + cookie);

    reader = new BufferedReader(new InputStreamReader(url.openStream()));
    String line = null;
    String result = "";
    while ((line = reader.readLine()) != null) {
    result += line;
    }
    reader.close();


    System.out.println("Result login: " + result);


    JSONObject obj = new JSONObject(result);
    String session = obj.getString("session");


    url = new URL("http://localhost:8084/ajax/calendar?action=all&session=" + session);
    System.out.println("URL3: " + url.toString());
    HttpURLConnection connCal = (HttpURLConnection) url.openConnection();
    connCal.setRequestProperty("Cookie", cookie);
    connCal.connect();

    reader = new BufferedReader(new InputStreamReader(connCal.getInputStream()));
    result = "";
    while ((line = reader.readLine()) != null) {
    result += line;
    }
    reader.close();

    System.out.println("Result calendars: " + result);

    return null;

    } catch (Exception ex) {
    ex.printStackTrace();
    } finally {
    try {
    reader.close();
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    return null;
    }

    I thought it was the right way to access via http-api, but my java console says this:

    cookie: open-xchange-session-9af7232dde2c45e5afb709e822551205=e534460f81cd406d9 d1dfec3ae73c64c

    Result login: {"session":"59e32f8b4a954e3787d9a897a177f07c","ran dom":"dc028e84f3bd4468b54255be234df536"}
    URL3: http://localhost:8084/ajax/calendar?...d9a897a177f07c

    Result calendars: {"code":"SES-0202","error_id":"1643285578-1","category":8,"error_params":[],"error":"The cookie with the session identifier is missing."}


    Can someone give me a code example, which corrects my failures? Thanks
    for any help,

    Jan

  • #2
    There are two Set-Cookie headers.

    Comment


    • #3
      Ok, thanks for your reply. That seemed to be a simple fault. Now I've got another one (and i hope for another simple one). I changed my login-method like
      that:

      ...

      Map<String, List<String>> headers = conn.getHeaderFields();

      Set<String> keys = headers.keySet();

      ArrayList<String> cookies = new ArrayList<String>();
      String cookieParam = "";
      for (String each : keys) {


      if (each != null && each.equals("Set-Cookie")) {
      List<String> values = headers.get(each);
      for (String eachV : values) {
      System.out.println("Set-Cookie: " + eachV);

      eachV = eachV.replace("path=/", "$Path=/; ");

      cookieParam += eachV;
      cookies.add(eachV);
      }
      }
      }

      reader = new BufferedReader(new InputStreamReader(url.openStream()));
      String line = null;
      String result = "";
      while ((line = reader.readLine()) != null) {
      result += line;
      }
      reader.close();


      System.out.println("Result login: " + result);


      JSONObject obj = new JSONObject(result);
      String session = obj.getString("session");


      url = new URL("http://localhost:8084/ajax/folders?action=root&columns=1,300&session=" + session);
      System.out.println("Request-URL with cookie: " + url.toString());
      HttpURLConnection connCal = (HttpURLConnection) url.openConnection();
      cookie = cookieParam;

      // ###### HERE STARTS THE COOKIE STUFF

      // The cookie as a single string (doesn't work)
      // System.out.println("cookie: " + cookie);
      // connCal.setRequestProperty("cookie", cookie);

      // add each Set-Cookie part as request-property (also doesn't work)
      for(String each : cookies) {
      connCal.addRequestProperty("cookie", each);
      System.out.println("cookie: " + each);
      }
      // ###### HERE FINISHES THE COOKIE STUFF

      connCal.connect();

      ...

      And now, thats my output. Writing the cookie-data as a single String-line into
      the header has the same effect.:

      Set-Cookie: JSESSIONID=70eda5d9fbc7477b98b901754253e44c.APP1; path=/
      Set-Cookie: open-xchange-session-22ae30aaa8eb4208aee07ef06fbc733e=50309f1c86ac45e28 f574d0e44b45d7d; path=/
      Result login: {"session":"a0d79515594047e4bc1d18249406dab9","ran dom":"6754f7d3c20d40a3b076f1ec7fcec3f7"}
      Request-URL with cookie: http://localhost:8084/ajax/folders?a...1d18249406dab9
      cookie: JSESSIONID=70eda5d9fbc7477b98b901754253e44c.APP1; $Path=/;
      cookie: open-xchange-session-22ae30aaa8eb4208aee07ef06fbc733e=50309f1c86ac45e28 f574d0e44b45d7d; $Path=/;
      Result calendars: {"code":"SES-0202","error_id":"-848029577-32","category":8,"error_params":[],"error":"The cookie with the session identifier is missing."}

      Can you probably give me an example (with my session values in this special
      case) how the cookie-parameter inside the request header should look like?

      Many thanks,
      Jan

      Comment


      • #4
        Just use java.net.CookieHandler

        Comment

        Working...
        X