/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.nutch.indexwriter.solr;


import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hadoop.mapred.JobConf;
import org.apache.http.impl.client.SystemDefaultHttpClient;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.CloudSolrClient;

import java.net.MalformedURLException;

public class SolrUtils {

  public static Logger LOG = LoggerFactory.getLogger(SolrUtils.class);
  private static HttpClient HTTP_CLIENT = new SystemDefaultHttpClient();

  /**
   *
   *
   * @param JobConf
   * @return SolrClient
   */
  public static ArrayList<SolrClient> getSolrClients(JobConf job) throws MalformedURLException {
    String[] urls = job.getStrings(SolrConstants.SERVER_URL);
    String[] zkHostString = job.getStrings(SolrConstants.ZOOKEEPER_HOSTS);
    ArrayList<SolrClient> solrClients = new ArrayList<SolrClient>();
    
    if (zkHostString != null && zkHostString.length > 0) {
      for (int i = 0; i < zkHostString.length; i++) {
        CloudSolrClient sc = getCloudSolrClient(zkHostString[i]);
        sc.setDefaultCollection(job.get(SolrConstants.COLLECTION));
        solrClients.add(sc);
      }
    } else {
      for (int i = 0; i < urls.length; i++) {
        SolrClient sc = new HttpSolrClient(urls[i], HTTP_CLIENT);
        solrClients.add(sc);
      }
    }

    return solrClients;
  }

  public static CloudSolrClient getCloudSolrClient(String url) throws MalformedURLException {
    CloudSolrClient sc = new CloudSolrClient(url.replace('|', ','), HTTP_CLIENT);
    sc.setParallelUpdates(true);
    sc.connect();
    return sc;
  }

  public static SolrClient getHttpSolrClient(String url) throws MalformedURLException {
    SolrClient sc =new HttpSolrClient(url, HTTP_CLIENT);
    return sc;
  }
  
  public static String stripNonCharCodepoints(String input) {
    StringBuilder retval = new StringBuilder();
    char ch;

    for (int i = 0; i < input.length(); i++) {
      ch = input.charAt(i);

      // Strip all non-characters
      // http://unicode.org/cldr/utility/list-unicodeset.jsp?a=[:Noncharacter_Code_Point=True:]
      // and non-printable control characters except tabulator, new line and
      // carriage return
      if (ch % 0x10000 != 0xffff && // 0xffff - 0x10ffff range step 0x10000
          ch % 0x10000 != 0xfffe && // 0xfffe - 0x10fffe range
          (ch <= 0xfdd0 || ch >= 0xfdef) && // 0xfdd0 - 0xfdef
          (ch > 0x1F || ch == 0x9 || ch == 0xa || ch == 0xd)) {

        retval.append(ch);
      }
    }

    return retval.toString();
  }

}

@@ -993,68 +993,8 @@
 nf;%0A
-import org.apache.http.impl.client.SystemDefaultHttpClient;%0A
 impo
@@ -1029,32 +1029,32 @@
 lrj.SolrClient;%0A
+
 import org.apach
@@ -1293,81 +1293,8 @@
 ss);
-%0A  private static HttpClient HTTP_CLIENT = new SystemDefaultHttpClient();
 %0A%0A  
@@ -2058,29 +2058,16 @@
 (urls%5Bi%5D
-, HTTP_CLIENT
 );%0A     
@@ -2294,29 +2294,16 @@
 %7C', ',')
-, HTTP_CLIENT
 );%0A    s
@@ -2452,24 +2452,24 @@
 Exception %7B%0A
+
     SolrClie
@@ -2501,21 +2501,8 @@
 (url
-, HTTP_CLIENT
 );%0A 

Revert botched commit of NUTCH-2267