<commit_before>//
// OMECredentials.java
//

/*
OME database I/O package for communicating with OME and OMERO servers.
Copyright (C) 2005-@year@ <NAME>, <NAME> and <NAME>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.ome.io;

import java.util.StringTokenizer;

/**
 * Stores credentials for logging into an OME/OMERO server.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/loci/ome/io/OMECredentials.java">Trac</a>,
 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/ome/io/OMECredentials.java">SVN</a></dd></dl>
 */
public class OMECredentials {

  // -- Fields --

  public String server;
  public String port;
  public String username;
  public String password;
  public long imageID;
  public boolean isOMERO;

  // -- Constructor --

  public OMECredentials(String server, String username, String password) {
    this.server = server;
    this.username = username;
    this.password = password;
  }

  /** Get credentials from a string. */
  public OMECredentials(String s) {
    if (s == null || s.trim().equals("")) {
      throw new IllegalArgumentException("Invalid credentials string");
    }

    if (s.indexOf("\n") != -1) {
      StringTokenizer st = new StringTokenizer(s, "\n");
      String token = null;
      while (st.hasMoreTokens()) {
        token = st.nextToken();
        String key = token.substring(0, token.indexOf("=")).trim();
        String value = token.substring(token.indexOf("=") + 1).trim();
        if (key.equals("server")) server = value;
        else if (key.equals("username")) username = value;
        else if (key.equals("port")) port = value;
        else if (key.equals("password")) password = value;
        else if (key.equals("id")) imageID = Long.parseLong(value);
      }
    }
    else {
      server = s.substring(0, s.lastIndexOf("?"));

      int first = server.indexOf(":");
      int last = server.lastIndexOf(":");
      if (server.indexOf("http://") == -1) {
        first = 0;
        if (last < 0) last = 0;
      }
      if (first != last) {
        port = server.substring(last + 1);
        server = server.substring(0, last);
      }

      int ndx = s.indexOf("&");
      username = s.substring(s.lastIndexOf("?") + 6, ndx);
      int end = s.indexOf("&", ndx + 1);
      if (end == -1) end = s.length();
      password = s.substring(ndx + 10, end);
      ndx = s.indexOf("&", ndx + 1);
      if (ndx > 0) imageID = Long.parseLong(s.substring(ndx + 4));<commit_msg>Better recognition of credentials strings.<commit_after>//
// OMECredentials.java
//

/*
OME database I/O package for communicating with OME and OMERO servers.
Copyright (C) 2005-@year@ <NAME>, <NAME> and <NAME>.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package loci.ome.io;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.StringTokenizer;

/**
 * Stores credentials for logging into an OME/OMERO server.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/loci/ome/io/OMECredentials.java">Trac</a>,
 * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/ome/io/OMECredentials.java">SVN</a></dd></dl>
 */
public class OMECredentials {

  // -- Fields --

  public String server;
  public String port;
  public String username;
  public String password;
  public long imageID;
  public boolean isOMERO;

  // -- Constructor --

  public OMECredentials(String server, String username, String password) {
    this.server = server;
    this.username = username;
    this.password = password;
  }

  /**
   * Get credentials from a string. The following two formats are recognized:
   * <code>ip.address?port=54321&username=login&password=<PASSWORD>&id=12345</code>
   * or:
   * <pre>
   * server=ip.address
   * port=54321
   * user=login
   * password=<PASSWORD>
   * id=12345
   * </pre>
   * Strings are assumed to be encoded with the HTML form encoding scheme,
   * and will be decoded accordingly.
   */
  public OMECredentials(String s) {
    final String invalidMsg = "Invalid credentials string";
    if (s == null) {
      throw new IllegalArgumentException(invalidMsg);
    }

    String split = s.indexOf("\n") < 0 ? "?&" : "\n";
    StringTokenizer st = new StringTokenizer(s, split);
    while (st.hasMoreTokens()) {
      String token = st.nextToken();

      int equals = token.indexOf("=");
      String key = equals < 0 ? "server" : token.substring(0, equals);
      String value = token.substring(equals + 1);

      try {
        key = URLDecoder.decode(key, "UTF-8").trim();
        value = URLDecoder.decode(value, "UTF-8").trim();
      }
      catch (UnsupportedEncodingException exc) {
        throw new IllegalArgumentException(invalidMsg, exc);
      }

      if (key.equals("server")) server = value;
      else if (key.equals("username")) username = value;
      else if (key.equals("port")) port = value;
      else if (key.equals("password")) password = <PASSWORD>;
      else if (key.equals("id")) {
        try {
          imageID = Long.parseLong(value);
        }
        catch (NumberFormatException exc) {
          throw new IllegalArgumentException(invalidMsg, exc);
        }
      }
{'fraction_non_alphanumeric': 0.12949090353157008, 'fraction_numerical': 0.012536309432808439, 'mean_word_length': 3.119647355163728, 'pattern_counts': {'":': 2, '<': 43, '<?xml version=': 0, '>': 41, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2339689', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1936, 'n_words': 632}