Exemplo n.º 1
0
  /**
   * Decodes a SmtpTransport URI.
   *
   * <p>Possible forms:
   *
   * <pre>
   * smtp://user:password@server:port CONNECTION_SECURITY_NONE
   * smtp+tls://user:password@server:port CONNECTION_SECURITY_TLS_OPTIONAL
   * smtp+tls+://user:password@server:port CONNECTION_SECURITY_TLS_REQUIRED
   * smtp+ssl+://user:password@server:port CONNECTION_SECURITY_SSL_REQUIRED
   * smtp+ssl://user:password@server:port CONNECTION_SECURITY_SSL_OPTIONAL
   * </pre>
   */
  public static ServerSettings decodeUri(String uri) {
    String host;
    int port;
    ConnectionSecurity connectionSecurity;
    String authenticationType = AUTH_AUTOMATIC;
    String username = null;
    String password = null;

    URI smtpUri;
    try {
      smtpUri = new URI(uri);
    } catch (URISyntaxException use) {
      throw new IllegalArgumentException("Invalid SmtpTransport URI", use);
    }

    String scheme = smtpUri.getScheme();
    if (scheme.equals("smtp")) {
      connectionSecurity = ConnectionSecurity.NONE;
      port = 25;
    } else if (scheme.equals("smtp+tls")) {
      connectionSecurity = ConnectionSecurity.STARTTLS_OPTIONAL;
      port = 25;
    } else if (scheme.equals("smtp+tls+")) {
      connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED;
      port = 25;
    } else if (scheme.equals("smtp+ssl+")) {
      connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED;
      port = 465;
    } else if (scheme.equals("smtp+ssl")) {
      connectionSecurity = ConnectionSecurity.SSL_TLS_OPTIONAL;
      port = 465;
    } else {
      throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")");
    }

    host = smtpUri.getHost();

    if (smtpUri.getPort() != -1) {
      port = smtpUri.getPort();
    }

    if (smtpUri.getUserInfo() != null) {
      try {
        String[] userInfoParts = smtpUri.getUserInfo().split(":");
        username = URLDecoder.decode(userInfoParts[0], "UTF-8");
        if (userInfoParts.length > 1) {
          password = URLDecoder.decode(userInfoParts[1], "UTF-8");
        }
        if (userInfoParts.length > 2) {
          authenticationType = userInfoParts[2];
        }
      } catch (UnsupportedEncodingException enc) {
        // This shouldn't happen since the encoding is hardcoded to UTF-8
        throw new IllegalArgumentException("Couldn't urldecode username or password.", enc);
      }
    }

    return new ServerSettings(
        TRANSPORT_TYPE, host, port, connectionSecurity, authenticationType, username, password);
  }
Exemplo n.º 2
0
  /**
   * @param queryString a query string of the form n1=v1&n2=v2&... to decode. May be null.
   * @param acceptAmp -> "&amp;" if true, "&" if false
   * @return a Map of String[] indexed by name, an empty Map if the query string was null
   */
  public static Map<String, String[]> decodeQueryString(
      final CharSequence queryString, final boolean acceptAmp) {

    final Map<String, String[]> result = new TreeMap<String, String[]>();
    if (queryString != null) {
      final Matcher matcher =
          acceptAmp ? PATTERN_AMP.matcher(queryString) : PATTERN_NO_AMP.matcher(queryString);
      int matcherEnd = 0;
      while (matcher.find()) {
        matcherEnd = matcher.end();
        try {
          // Group 0 is the whole match, e.g. a=b, while group 1 is the first group
          // denoted ( with parens ) in the expression.  Hence we start with group 1.
          final String name =
              URLDecoder.decode(matcher.group(1), NetUtils.STANDARD_PARAMETER_ENCODING);
          final String value =
              URLDecoder.decode(matcher.group(2), NetUtils.STANDARD_PARAMETER_ENCODING);

          StringUtils.addValueToStringArrayMap(result, name, value);
        } catch (UnsupportedEncodingException e) {
          // Should not happen as we are using a required encoding
          throw new OXFException(e);
        }
      }
      if (queryString.length() != matcherEnd) {
        // There was garbage at the end of the query.
        throw new OXFException("Malformed URL: " + queryString);
      }
    }
    return result;
  }
Exemplo n.º 3
0
  public static String getAddressByIP(String ip) {
    try {
      String js = visitWeb("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=" + ip);
      JsonParser jsonParser = new JsonParser();
      js = js.trim();
      JsonObject jo = jsonParser.parse(js.substring(21, js.length() - 1)).getAsJsonObject();
      String province = "";
      String city = "";
      try {
        // 获取 省、市
        province =
            jo.get("province") == null
                ? ""
                : URLDecoder.decode(jo.get("province").toString(), "UTF-8");
        city = jo.get("city") == null ? "" : URLDecoder.decode(jo.get("city").toString(), "UTF-8");
        // 省为空用国家代替
        if (StringUtils.isEmpty(province)) {
          province =
              jo.get("country") == null
                  ? ""
                  : URLDecoder.decode(jo.get("country").toString(), "UTF-8");
        }

      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }

      return (province.equals("") || province.equals(city)) ? city : province + " " + city;
    } catch (Exception e) {
      e.printStackTrace();
      return "";
    }
  }
  public static int rollbackGUIConfigurationTransaction(String sConfigurationFileName) {

    String sConfigurationLockFile;
    File fFile;

    sConfigurationLockFile = new String(sConfigurationFileName + ".lck");

    try {
      fFile = new File(URLDecoder.decode(sConfigurationLockFile, "UTF-8"));
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null, "URLDecoder.decode failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    if (fFile.delete() == false) {
      JOptionPane.showMessageDialog(
          null,
          "fFile.delete on " + sConfigurationLockFile + " failed",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 3;
    }

    return 0;
  }
Exemplo n.º 5
0
 /**
  * A partir de uma string com o conteudo de um form submetido no formato
  * application/x-www-form-urlencoded, devolve um objecto do tipo Properties, associando a cada
  * elemento do form o seu valor
  */
 public static Properties parseHttpPostContents(String contents) throws IOException {
   Properties props = new Properties();
   Scanner scanner = new Scanner(contents).useDelimiter("&");
   while (scanner.hasNext()) {
     Scanner inScanner = new Scanner(scanner.next()).useDelimiter("=");
     String propName = URLDecoder.decode(inScanner.next(), "UTF-8");
     String propValue = "";
     try {
       propValue = URLDecoder.decode(inScanner.next(), "UTF-8");
     } catch (Exception e) {
       // do nothing
     }
     props.setProperty(propName, propValue);
   }
   return props;
 }
Exemplo n.º 6
0
  /**
   * This method takes a string, and replaces all file separators and space etc by normal
   * characters.
   */
  private String niceifyLink(String link) {
    link = URLDecoder.decode(link);
    if (link.equals("/")) return "index.html";

    int i1, i2;
    // trim off the project path if it exists
    String testDir = parent.getProperty("qat.project.path"); // "/local/workspaces/QAT/examples"
    i1 = link.indexOf(testDir);
    if (i1 >= 0) {
      link = link.substring(0, i1) + link.substring(i1 + testDir.length() + 1, link.length());
    }

    // trim off the qat basedir path if it exists
    String projectResultsDir =
        parent.getProjectResultsDirectory(); // "/home/webhiker/.qat/harness/results/examples"
    i1 = link.indexOf(projectResultsDir);
    if (i1 >= 0) {
      link =
          link.substring(0, i1)
              + link.substring(i1 + projectResultsDir.length() + 1, link.length());
    }

    link = link.replace('\\', '_').replace('/', '_').replace(' ', '_').replace(':', '_') + ".html";
    return link;
  }
Exemplo n.º 7
0
 /**
  * URL decodes a string
  *
  * @param string percent encoded string
  * @return plain string
  */
 public static String urlDecodeWrapper(String string) {
   try {
     return URLDecoder.decode(string, UTF_8);
   } catch (UnsupportedEncodingException uee) {
     throw new IllegalStateException(ERROR_MSG, uee);
   }
 }
Exemplo n.º 8
0
 /**
  * URL decodes a string
  *
  * @param string percent encoded string
  * @return plain string
  */
 public static String urlDecodeWrapper(String string) {
   Preconditions.checkNotNull(string, "Cannot decode null string");
   try {
     return URLDecoder.decode(string, UTF_8);
   } catch (UnsupportedEncodingException uee) {
     throw new IllegalStateException(ERROR_MSG, uee);
   }
 }
Exemplo n.º 9
0
  // 응용프로그램에서 이미지 출력을 하기 위한 메소드
  public void showImage() throws Exception {
    for (int i = 0; i < vUnparsedEntityDecl.size(); i++) {
      UnparsedEntityDecl ued = (UnparsedEntityDecl) vUnparsedEntityDecl.elementAt(i);
      URL urlImageFile = new URL(ued.systemId);
      String imageFile = URLDecoder.decode(urlImageFile.getFile(), "euc-kr");
      imageFile = imageFile.replaceAll("/C:", "C:");

      NotationDecl nd = (NotationDecl) hNotationDecl.get(ued.notationName);
      URL urlHelperProgram = new URL(nd.systemId);
      String helperProgram = URLDecoder.decode(urlHelperProgram.getFile(), "euc-kr");
      helperProgram = helperProgram.replaceAll("/C:", "C:");

      String command = helperProgram + " " + imageFile;
      System.out.println(command);
      Process process = Runtime.getRuntime().exec(command);
    }
  }
Exemplo n.º 10
0
 /**
  * Decodes the specified path segments.
  *
  * @param segments strings to be decoded
  * @return argument
  * @throws IllegalArgumentException invalid path segments
  */
 public static String[] decode(final String[] segments) {
   try {
     final int sl = segments.length;
     for (int s = 0; s < sl; s++) {
       segments[s] = URLDecoder.decode(segments[s], Prop.ENCODING);
     }
     return segments;
   } catch (final UnsupportedEncodingException ex) {
     throw new IllegalArgumentException(ex);
   }
 }
Exemplo n.º 11
0
 /**
  * Adds parameters from the passed on request body.
  *
  * @param body request body
  * @param params map parameters
  */
 private static void addParams(final String body, final Map<String, String[]> params) {
   for (final String nv : body.split("&")) {
     final String[] parts = nv.split("=", 2);
     if (parts.length < 2) continue;
     try {
       params.put(parts[0], new String[] {URLDecoder.decode(parts[1], Token.UTF8)});
     } catch (final Exception ex) {
       Util.notexpected(ex);
     }
   }
 }
Exemplo n.º 12
0
 public static void processSingle(String data, Map<String, String> variables) throws Exception {
   if (data != null && !data.equals("")) {
     String[] items = data.split("&");
     for (int x = 0; x < items.length; x++) {
       int splitter = items[x].indexOf('=');
       String variable = items[x].substring(0, splitter);
       String value = items[x].substring(splitter + 1);
       variables.put(variable, URLDecoder.decode(value, "UTF-8"));
     }
   }
 }
Exemplo n.º 13
0
  /**
   * This method will take a file name and try to "decode" any URL encoded characters. For example
   * if the file name contains any spaces this method call will take the resulting %20 encoded
   * values and convert them to spaces.
   *
   * <p>This method was added specifically to fix defect: Groovy-1787. The defect involved a
   * situation where two scripts were sitting in a directory with spaces in its name. The code would
   * fail when the class loader tried to resolve the file name and would choke on the URLEncoded
   * space values.
   */
  private String decodeFileName(String fileName) {
    String decodedFile = fileName;
    try {
      decodedFile = URLDecoder.decode(fileName, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      System.err.println(
          "Encountered an invalid encoding scheme when trying to use URLDecoder.decode() inside of the GroovyClassLoader.decodeFileName() method.  Returning the unencoded URL.");
      System.err.println(
          "Please note that if you encounter this error and you have spaces in your directory you will run into issues.  Refer to GROOVY-1787 for description of this bug.");
    }

    return decodedFile;
  }
Exemplo n.º 14
0
 /**
  * Get the last modification date of an open URLConnection.
  *
  * <p>This handles the (broken at some point in the Java libraries) case of the file: protocol.
  *
  * @return last modified timestamp "as is"
  */
 public static long getLastModified(URLConnection urlConnection) {
   try {
     long lastModified = urlConnection.getLastModified();
     if (lastModified == 0 && "file".equals(urlConnection.getURL().getProtocol()))
       lastModified =
           new File(
                   URLDecoder.decode(
                       urlConnection.getURL().getFile(), STANDARD_PARAMETER_ENCODING))
               .lastModified();
     return lastModified;
   } catch (UnsupportedEncodingException e) {
     // Should not happen as we are using a required encoding
     throw new OXFException(e);
   }
 }
Exemplo n.º 15
0
 public static Vector expandFileList(String[] files, boolean inclDirs) {
   Vector v = new Vector();
   if (files == null) return v;
   for (int i = 0; i < files.length; i++) v.add(new File(URLDecoder.decode(files[i])));
   for (int i = 0; i < v.size(); i++) {
     File f = (File) v.get(i);
     if (f.isDirectory()) {
       File[] fs = f.listFiles();
       for (int n = 0; n < fs.length; n++) v.add(fs[n]);
       if (!inclDirs) {
         v.remove(i);
         i--;
       }
     }
   }
   return v;
 }
Exemplo n.º 16
0
 /**
  * Get the last modification date of a URL.
  *
  * @return last modified timestamp "as is"
  */
 public static long getLastModified(URL url) throws IOException {
   if ("file".equals(url.getProtocol())) {
     // Optimize file: access. Also, this prevents throwing an exception if the file doesn't exist
     // as we try to close the stream below.
     return new File(URLDecoder.decode(url.getFile(), STANDARD_PARAMETER_ENCODING)).lastModified();
   } else {
     // Use URLConnection
     final URLConnection urlConnection = url.openConnection();
     if (urlConnection instanceof HttpURLConnection)
       ((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
     try {
       return getLastModified(urlConnection);
     } finally {
       final InputStream is = urlConnection.getInputStream();
       if (is != null) is.close();
     }
   }
 }
Exemplo n.º 17
0
 /**
  * Parse the query string.
  *
  * @param map parsed key-values will be stored here
  * @param qs query string
  */
 private static void parseQueryString(final Map<String, String> map, final String qs) {
   if (qs == null) return;
   for (final String nv : qs.split("&")) {
     final String[] parts = nv.split("=");
     final String key = parts[0];
     String val = null;
     if (parts.length > 1) {
       val = parts[1];
       if (val != null) {
         try {
           val = URLDecoder.decode(val, Token.UTF8);
         } catch (final UnsupportedEncodingException ex) {
           throw new RuntimeException(ex);
         }
       }
     }
     map.put(key, val);
   }
 }
Exemplo n.º 18
0
 public static void GetDownloadIndex() throws Exception {
   BufferedReader in = new BufferedReader(new InputStreamReader(ylink.openStream()));
   // BufferedWriter out = new BufferedWriter(new FileWriter("output.html"));
   int b;
   String op = null;
   while ((b = in.read()) != -1) {
     //	out.write(b);
     op = op + (char) b;
   }
   String[] list = op.split("url=");
   for (String i : list) {
     if (i.startsWith("https")) {
       set.add(i.split(",")[0]);
     }
   }
   String url = null, itag = null, quality_label = null, type = null;
   for (String i : set) {
     String temp = i;
     temp = temp.replace("\\", "\\\\");
     for (String sdata : temp.split("\\\\")) {
       if (sdata.startsWith("https")) url = sdata;
     }
     if (url != null) {
       if (url.startsWith("https")) {
         url = url.split(" ")[0];
         url = URLDecoder.decode(url, "UTF-8");
         if (url.contains("mime=")) type = (url.split("mime=")[1]).split("&")[0];
         if (url.contains("itag")) itag = (url.split("itag=")[1]).split("&")[0];
         if (url.contains("&gir=yes")) {
           if (type.startsWith("video"))
             video_set.add(url + "\ntype: " + type + "\nitag: " + itag);
           else if (type.startsWith("audio"))
             audio_set.add(url + "\ntype: " + type + "\nitag: " + itag);
         } else av_set.add(url + "\ntype: " + type + "\nitag: " + itag);
       }
     }
   }
 }
Exemplo n.º 19
0
  /** @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) */
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    String inputString = request.getParameter("data");
    String mapType = request.getParameter("type");

    response.setContentType("text/text");
    PrintWriter out = response.getWriter();

    boolean isLatLon = false;

    if (inputString == null || inputString.length() < 10) {
      out.write("Error: not enough input data");
      out.close();
      return;
    }

    inputString = (URLDecoder.decode(inputString, "UTF-8"));

    String pStr[][] = parseCSV.parse(inputString);

    if (pStr == null) {
      out.write("Error: problem with input data");
      out.close();
      return;
    }

    if (!this.dynamicMap.containsKey(pStr[0][0])) {
      int pStrLen = pStr[0].length;

      if (pStr[0][pStrLen - 1].isEmpty()) pStrLen--;

      String cLon = pStr[0][pStrLen - 2];
      String cLat = pStr[0][pStrLen - 1];

      if (cLon.equalsIgnoreCase("lon") && cLat.equalsIgnoreCase("lat")) {

        isLatLon = true;

      } else {
        out.write("Error: 1st column does not match any Mapped column");
        out.close();
        return;
      }
    }

    metaDataCtl mdc =
        new metaDataCtl(
            this.db_host, this.db_user, this.db_password, this.db_name, this.db_metadata);

    Random rand = new Random();

    String sessionID = request.getSession(true).getId();
    String tableName = "mb_" + sessionID + "_" + rand.nextInt(10000);

    if (!mdc.setMetaInfo(pStr[0], this.dynamicMap, this.db_metadata, tableName, isLatLon)) {
      out.write("Error: Catalog entry");
      out.close();
      return;
    }

    if (isLatLon) {
      if (!mdc.processDataLatLon(pStr, tableName)) {
        out.write("Error: Data entry");
        out.close();
        return;
      }
    } else {
      if (!mdc.processData(pStr, tableName)) {
        out.write("Error: Data entry");
        out.close();
        return;
      }
    }

    StringBuffer sb = new StringBuffer();
    sb.append(UrlUtil.getUrl(request));
    if (mapType != null && mapType.compareToIgnoreCase("zd") == 0) sb.append("/zshowtheme?layer=");
    else sb.append("/showtheme?layer=");
    sb.append(tableName);

    out.write(sb.toString());
    out.close();
  }
  public static int commitGUIConfigurationTransaction(
      XMLTreeForConfiguration xtfcXMLTreeForConfiguration, Vector vFunctionalities) {

    TreeVisit tvConfigurationTreeVisit;
    TreeVisitToGetConfigurationFile tvgcTreeVisitToGetConfigurationFile;
    String sConfigurationFile;
    URL uXML;
    String sConfigurationLockFile;
    BufferedWriter bwBufferedWriter;
    File fFile;

    tvgcTreeVisitToGetConfigurationFile = new TreeVisitToGetConfigurationFile(vFunctionalities);
    tvConfigurationTreeVisit = new TreeVisit(tvgcTreeVisitToGetConfigurationFile);

    if (tvConfigurationTreeVisit.inOrderVisit(xtfcXMLTreeForConfiguration.getXMLTopTreeComponent())
        != 0) {
      JOptionPane.showMessageDialog(
          null, "inOrderVisit failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    sConfigurationFile = tvgcTreeVisitToGetConfigurationFile.getConfigurationFile();

    System.out.println("commitGUIConfigurationTransaction: " + sConfigurationFile);

    uXML = xtfcXMLTreeForConfiguration.getXML();

    try {
      bwBufferedWriter =
          new BufferedWriter(new FileWriter(URLDecoder.decode(uXML.getFile(), "UTF-8")));
      bwBufferedWriter.write(sConfigurationFile, 0, sConfigurationFile.length());
      bwBufferedWriter.close();

      System.out.println(
          "commitGUIConfigurationTransaction: file written ("
              + URLDecoder.decode(uXML.getFile(), "UTF-8")
              + ")");
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null,
          "Operation on BufferedWriter failed (3)",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 2;
    }

    sConfigurationLockFile = new String(uXML.getFile() + ".lck");

    try {
      fFile = new File(URLDecoder.decode(sConfigurationLockFile, "UTF-8"));

      System.out.println("File. delete: " + URLDecoder.decode(sConfigurationLockFile, "UTF-8"));
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null, "URLDecoder.decode failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    if (fFile.delete() == false) {
      JOptionPane.showMessageDialog(
          null,
          "fFile.delete on " + sConfigurationLockFile + " failed",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 3;
    }

    return 0;
  }
 @SuppressWarnings("deprecation")
 private static String decodeUrl(String file) {
   String quotePluses = StringUtil.replace(file, "+", "%2B");
   return URLDecoder.decode(quotePluses);
 }
Exemplo n.º 22
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType(XML_RESPONSE_HEADER); // Talkback happens in XML form.
    response.setCharacterEncoding("UTF-8"); // Unicode++
    request.setCharacterEncoding("UTF-8");

    PrintWriter out = null; // The talkback buffer.

    // handle startrecord
    Integer startRecord = 0;

    if (!(request.getParameter("startRecord") == null)) {
      try {
        startRecord = Integer.parseInt(request.getParameter("startRecord")) - 1;
      } catch (NumberFormatException e) {
        startRecord = 0;
      }
    }

    // maximumrecords
    Integer maximumRecords = Integer.parseInt(this.config.getProperty("default_maximumRecords"));
    if (!(request.getParameter("maximumRecords") == null)) {
      maximumRecords = Integer.parseInt(request.getParameter("maximumRecords"));
    }

    // operation
    String operation = request.getParameter("operation");

    // x_collection
    String x_collection = request.getParameter("x-collection");
    if (x_collection == null) x_collection = this.config.getProperty("default_x_collection");
    if (x_collection == null) operation = null;

    // sortkeys
    String sortKeys = request.getParameter("sortKeys");

    // sortorder
    String sortOrder = request.getParameter("sortOrder");

    // recordschema
    String recordSchema = request.getParameter("recordSchema");
    if (recordSchema == null) recordSchema = "dc";

    if (recordSchema.equalsIgnoreCase("dcx")) {
      recordSchema = "dcx";
    }

    if (recordSchema.equalsIgnoreCase("solr")) {
      recordSchema = "solr";
    }

    // query request
    String query = request.getParameter("query");
    String q = request.getParameter("q");

    // who is requestor ?
    String remote_ip = request.getHeader("X-FORWARDED-FOR");

    if (remote_ip == null) {
      remote_ip = request.getRemoteAddr().trim();
    } else {
      remote_ip = request.getHeader("X-FORWARDED-FOR");
    }

    // handle debug
    Boolean debug = Boolean.parseBoolean(request.getParameter("debug"));
    if (!debug) {
      out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
    }

    // handle query
    if ((query == null) && (q != null)) {
      query = q;
    } else {
      if ((query != null) && (q == null)) {
        q = query;
      } else {
        operation = null;
      }
    }

    // handle operation
    if (operation == null) {
      if (query != null) {
        operation = "searchRetrieve";
      } else {
        operation = "explain";
      }
    }

    //  searchRetrieve
    if (operation.equalsIgnoreCase("searchRetrieve")) {
      if (query == null) {
        operation = "explain";
        log.debug(operation + ":" + query);
      }
    }

    // start talking back.
    String[] sq = {""};
    String solrquery = "";

    // facet

    String facet = null;
    List<FacetField> fct = null;

    if (request.getParameter("facet") != null) {
      facet = request.getParameter("facet");
      log.debug("facet : " + facet);
    }

    if (operation == null) {
      operation = "searchretrieve";
    } else { // explain response
      if (operation.equalsIgnoreCase("explain")) {
        log.debug("operation = explain");
        out.write("<srw:explainResponse xmlns:srw=\"http://www.loc.gov/zing/srw/\">");
        out.write("</srw:explainResponse>");
      } else { // DEBUG routine
        operation = "searchretrieve";

        String triplequery = null;

        if (query.matches(".*?\\[.+?\\].*?")) { // New symantic syntax
          triplequery = symantic_query(query);
          query = query.split("\\[")[0] + " " + triplequery;
          log.fatal(triplequery);

          solrquery = CQLtoLucene.translate(query, log, config);
        } else {
          solrquery = CQLtoLucene.translate(query, log, config);
        }
        log.debug(solrquery);

        if (debug == true) {
          response.setContentType(HTML_RESPONSE_HEADER);
          out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), "UTF8"), true);
          out.write("<html><body>\n\n");
          out.write("'" + remote_ip + "'<br>\n");
          out.write("<form action='http://www.kbresearch.nl/kbSRU'>");
          out.write("<input type=text name=q value='" + query + "' size=120>");
          out.write("<input type=hidden name=debug value=True>");
          out.write("<input type=submit>");
          out.write("<table border=1><tr><td>");
          out.write("q</td><td>" + query + "</td></tr><tr>");
          out.write("<td>query out</td><td>" + URLDecoder.decode(solrquery) + "</td></tr>");
          out.write(
              "<tr><td>SOLR_URL</td><td> <a href='"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/?q="
                  + solrquery
                  + "'>"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/select/?q="
                  + solrquery
                  + "</a><br>"
                  + this.config.getProperty("solr_url")
                  + solrquery
                  + "</td></tr>");
          out.write(
              "<b>SOLR_QUERY</b> : <BR> <iframe width=900 height=400 src='"
                  + this.config.getProperty(
                      "collection." + x_collection.toLowerCase() + ".solr_baseurl")
                  + "/../?q="
                  + solrquery
                  + "'></iframe><BR>");
          out.write(
              "<b>SRU_QUERY</b> : <BR> <a href="
                  + this.config.getProperty("baseurl")
                  + "?q="
                  + query
                  + "'>"
                  + this.config.getProperty("baseurl")
                  + "?q="
                  + query
                  + "</a><br><iframe width=901 height=400 src='http://www.kbresearch.nl/kbSRU/?q="
                  + query
                  + "'></iframe><BR>");
          out.write(
              "<br><b>JSRU_QUERY</b> : <BR><a href='http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection="
                  + x_collection
                  + "'>http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection=GGC</a><br><iframe width=900 height=400 src='http://jsru.kb.nl/sru/?query="
                  + query
                  + "&x-collection=GGC'></iframe>");

        } else { // XML SearchRetrieve response
          String url =
              this.config.getProperty("collection." + x_collection.toLowerCase() + ".solr_baseurl");
          String buffer = "";
          CommonsHttpSolrServer server = null;
          server = new CommonsHttpSolrServer(url);
          log.fatal("URSING " + url);
          server.setParser(new XMLResponseParser());
          int numfound = 0;
          try {
            SolrQuery do_query = new SolrQuery();
            do_query.setQuery(solrquery);
            do_query.setRows(maximumRecords);
            do_query.setStart(startRecord);

            if ((sortKeys != null) && (sortKeys.length() > 1)) {
              if (sortOrder != null) {
                if (sortOrder.equals("asc")) {
                  do_query.setSortField(sortKeys, SolrQuery.ORDER.asc);
                }
                if (sortOrder.equals("desc")) {
                  do_query.setSortField(sortKeys, SolrQuery.ORDER.desc);
                }
              } else {
                for (String str : sortKeys.trim().split(",")) {
                  str = str.trim();
                  if (str.length() > 1) {
                    if (str.equals("date")) {
                      do_query.setSortField("date_date", SolrQuery.ORDER.desc);
                      log.debug("SORTORDERDEBUG | DATE! " + str + " | ");
                      break;
                    } else {
                      do_query.setSortField(str + "_str", SolrQuery.ORDER.asc);
                      log.debug("SORTORDERDEBUG | " + str + " | ");
                      break;
                    }
                  }
                }
              }
            }

            if (facet != null) {
              if (facet.indexOf(",") > 1) {
                for (String str : facet.split(",")) {
                  if (str.indexOf("date") > 1) {
                    do_query.addFacetField(str);
                  } else {
                    do_query.addFacetField(str);
                  }
                  // do_query.setParam("facet.method", "enum");
                }
                // q.setFacetSort(false);
              } else {
                do_query.addFacetField(facet);
              }
              do_query.setFacet(true);
              do_query.setFacetMinCount(1);
              do_query.setFacetLimit(-1);
            }

            log.fatal(solrquery);

            QueryResponse rsp = null;
            boolean do_err = false;
            boolean do_sugg = false;
            SolrDocumentList sdl = null;
            String diag = "";
            StringBuffer suggest = new StringBuffer("");

            String content = "1";

            SolrQuery spellq = do_query;
            try {
              rsp = server.query(do_query);
            } catch (SolrServerException e) {
              String header = this.SRW_HEADER.replaceAll("\\$numberOfRecords", "0");
              out.write(header);
              diag = this.SRW_DIAG.replaceAll("\\$error", e.getMessage());
              do_err = true;
              rsp = null;
            }

            log.fatal("query done..");
            if (!(do_err)) { // XML dc response

              SolrDocumentList docs = rsp.getResults();
              numfound = (int) docs.getNumFound();
              int count = startRecord;
              String header =
                  this.SRW_HEADER.replaceAll("\\$numberOfRecords", Integer.toString(numfound));
              out.write(header);
              out.write("<srw:records>");

              Iterator<SolrDocument> iter = rsp.getResults().iterator();

              while (iter.hasNext()) {
                count += 1;
                if (recordSchema.equalsIgnoreCase("dc")) {
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                  out.write(
                      "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" xmlns:facets=\"info:srw/extension/4/facets\" >");
                  StringBuffer result = new StringBuffer("");

                  construct_lucene_dc(result, resultDoc);

                  out.write(result.toString());
                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }

                if (recordSchema.equalsIgnoreCase("solr")) {
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/solr</srw:recordSchema>");
                  out.write("<srw:recordData xmlns:expand=\"http://www.kbresearch.nl/expand\">");
                  StringBuffer result = new StringBuffer("");
                  construct_lucene_solr(result, resultDoc);
                  out.write(result.toString());

                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }

                if (recordSchema.equalsIgnoreCase("dcx")) { // XML dcx response
                  out.write("<srw:record>");
                  out.write("<srw:recordPacking>xml</srw:recordPacking>");
                  out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                  out.write(
                      "<srw:recordData><srw_dc:dc xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/marc.relators/\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                  SolrDocument resultDoc = iter.next();
                  content = (String) resultDoc.getFieldValue("id");

                  String dcx_data =
                      helpers.getOAIdcx(
                          "http://services.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                          log);
                  if (x_collection.equalsIgnoreCase("ggc-thes")) {
                    dcx_data =
                        helpers.getOAIdcx(
                            "http://serviceso.kb.nl/mdo/oai?verb=GetRecord&identifier=" + content,
                            log);
                  }

                  if (!(dcx_data.length() == 0)) {
                    out.write(dcx_data);
                  } else {
                    // Should not do this!!

                    out.write("<srw:record>");
                    out.write("<srw:recordPacking>xml</srw:recordPacking>");
                    out.write("<srw:recordSchema>info:srw/schema/1/dc-v1.1</srw:recordSchema>");
                    out.write(
                        "<srw:recordData xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:mods=\"http://www.loc.gov/mods\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:dcx=\"http://krait.kb.nl/coop/tel/handbook/telterms.html\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:marcrel=\"http://www.loc.gov/loc.terms/relators/OTH\" >");
                    StringBuffer result = new StringBuffer("");

                    construct_lucene_dc(result, resultDoc);

                    out.write(result.toString());
                    out.write("</srw:recordData>");
                    out.write(
                        "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                    out.write("</srw:record>");
                  }

                  out.write("</srw_dc:dc>");

                  StringBuffer expand_data;
                  boolean expand = false;

                  if (content.startsWith("GGC-THES:AC:")) {
                    String tmp_content = "";
                    tmp_content = content.replaceFirst("GGC-THES:AC:", "");
                    log.fatal("calling get");
                    expand_data =
                        new StringBuffer(
                            helpers.getExpand(
                                "http://www.kbresearch.nl/general/lod_new/get/"
                                    + tmp_content
                                    + "?format=rdf",
                                log));
                    log.fatal("get finini");

                    if (expand_data.toString().length() > 4) {

                      out.write(
                          "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                      out.write(expand_data.toString());
                      expand = true;
                    }
                  } else {
                    expand_data =
                        new StringBuffer(
                            helpers.getExpand(
                                "http://www.kbresearch.nl/ANP.cgi?q=" + content, log));
                    if (expand_data.toString().length() > 0) {
                      if (!expand) {
                        out.write(
                            "<srw_dc:expand xmlns:srw_dc=\"info:srw/schema/1/dc-v1.1\" xmlns:expand=\"http://www.kbresearch.nl/expand\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" >");
                        expand = true;
                      }
                      out.write(expand_data.toString());
                    }
                  }
                  if (expand) {
                    out.write("</srw_dc:expand>");
                  }

                  out.write("</srw:recordData>");
                  out.write(
                      "<srw:recordPosition>" + Integer.toString(count) + "</srw:recordPosition>");
                  out.write("</srw:record>");
                }
              }
            }

            if ((do_err) || (numfound == 0)) {
              log.fatal("I haz suggestions");

              try {
                spellq.setParam("spellcheck", true);
                spellq.setQueryType("/spell");
                server = new CommonsHttpSolrServer(url);
                rsp = server.query(spellq);
                sdl = rsp.getResults();
                SpellCheckResponse spell;
                spell = rsp.getSpellCheckResponse();
                List<SpellCheckResponse.Suggestion> suggestions = spell.getSuggestions();
                if (suggestions.isEmpty() == false) {
                  suggest.append("<srw:extraResponseData>");
                  suggest.append("<suggestions>");

                  for (SpellCheckResponse.Suggestion sugg : suggestions) {
                    suggest.append("<suggestionfor>" + sugg.getToken() + "</suggestionfor>");
                    for (String item : sugg.getSuggestions()) {
                      suggest.append("<suggestion>" + item + "</suggestion>");
                    }
                    suggest.append("</suggestions>");
                    suggest.append("</srw:extraResponseData>");
                  }
                  do_sugg = true;
                }
              } catch (Exception e) {
                rsp = null;
                // log.fatal(e.toString());
              }
              ;
            }
            ;

            if (!do_err) {
              if (facet != null) {

                try {
                  fct = rsp.getFacetFields();
                  out.write("<srw:facets>");

                  for (String str : facet.split(",")) {
                    out.write("<srw:facet>");
                    out.write("<srw:facetType>");
                    out.write(str);
                    out.write("</srw:facetType>");

                    for (FacetField f : fct) {
                      log.debug(f.getName());
                      // if (f.getName().equals(str+"_str") || (f.getName().equals(str+"_date")) ) {
                      List<FacetField.Count> facetEnties = f.getValues();
                      for (FacetField.Count fcount : facetEnties) {
                        out.write("<srw:facetValue>");
                        out.write("<srw:valueString>");
                        out.write(helpers.xmlEncode(fcount.getName()));
                        out.write("</srw:valueString>");
                        out.write("<srw:count>");
                        out.write(Double.toString(fcount.getCount()));
                        out.write("</srw:count>");
                        out.write("</srw:facetValue>");
                        //   }
                      }
                    }
                    out.write("</srw:facet>");
                  }
                  out.write("</srw:facets>");
                  startRecord += 1;
                } catch (Exception e) {
                }

                // log.fatal(e.toString()); }
              }
            } else {
              out.write(diag);
            }
            out.write("</srw:records>"); // SearchRetrieve response footer
            String footer = this.SRW_FOOTER.replaceAll("\\$query", helpers.xmlEncode(query));
            footer = footer.replaceAll("\\$startRecord", (startRecord).toString());
            footer = footer.replaceAll("\\$maximumRecords", maximumRecords.toString());
            footer = footer.replaceAll("\\$recordSchema", recordSchema);
            if (do_sugg) {
              out.write(suggest.toString());
            }
            out.write(footer);
          } catch (MalformedURLException e) {
            out.write(e.getMessage());
          } catch (IOException e) {
            out.write("TO ERR is Human");
          }
        }
      }
    }
    out.close();
  }
  public static int beginGUIConfigurationTransaction(String sConfigurationFileName, String sUser) {

    String sConfigurationLockFile;
    BufferedWriter bwBufferedWriter;
    File fFile;

    sConfigurationLockFile = new String(sConfigurationFileName + ".lck");

    System.out.println("beginGUIConfigurationTransaction: " + sConfigurationFileName);

    try {
      fFile = new File(URLDecoder.decode(sConfigurationLockFile, "UTF-8"));
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null, "URLDecoder.decode failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

      return 1;
    }

    if (fFile.exists()) {
      BufferedReader brBufferedReader;
      char cLastConfigurationUser[];

      cLastConfigurationUser = new char[((int) fFile.length())];

      try {
        brBufferedReader =
            new BufferedReader(new FileReader(URLDecoder.decode(sConfigurationLockFile, "UTF-8")));
        brBufferedReader.read(cLastConfigurationUser, 0, (int) (fFile.length()));
        brBufferedReader.close();

        System.out.println(
            "Last configuration user: "******"Operation on BufferedWriter failed (1)",
            "ConfigurationTransaction",
            JOptionPane.ERROR_MESSAGE);

        return 1;
      }

      if (String.copyValueOf(cLastConfigurationUser).compareTo(sUser) == 0) {
        System.out.println("File. delete: " + sConfigurationFileName);

        if (fFile.delete() == false) {
          JOptionPane.showMessageDialog(
              null, "fFile.delete failed", "ConfigurationTransaction", JOptionPane.ERROR_MESSAGE);

          return 2;
        }
      } else {
        JOptionPane.showMessageDialog(
            null,
            "The configuration is locked by "
                + String.copyValueOf(cLastConfigurationUser)
                + ".\nYou cannot change the configuration.",
            "ConfigurationTransaction",
            JOptionPane.PLAIN_MESSAGE);

        return 3;
      }
    }

    try {
      bwBufferedWriter =
          new BufferedWriter(new FileWriter(URLDecoder.decode(sConfigurationLockFile, "UTF-8")));
      bwBufferedWriter.write(sUser, 0, sUser.length());
      bwBufferedWriter.close();

      System.out.println("Configuration user written: " + sUser);
    } catch (IOException e) {
      JOptionPane.showMessageDialog(
          null,
          "Operation on BufferedWriter failed (" + e + ")",
          "ConfigurationTransaction",
          JOptionPane.ERROR_MESSAGE);

      return 4;
    }

    return 0;
  }