~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/util/SimplePostTool.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.solr.util;
2
 
 
3
 
/**
4
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
5
 
 * contributor license agreements.  See the NOTICE file distributed with
6
 
 * this work for additional information regarding copyright ownership.
7
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
8
 
 * (the "License"); you may not use this file except in compliance with
9
 
 * the License.  You may obtain a copy of the License at
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
13
 
 * Unless required by applicable law or agreed to in writing, software
14
 
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 
 * See the License for the specific language governing permissions and
17
 
 * limitations under the License.
18
 
 */
19
 
 
20
 
import java.io.File;
21
 
import java.io.FileInputStream;
22
 
import java.io.IOException;
23
 
import java.io.InputStream;
24
 
import java.io.ByteArrayInputStream;
25
 
import java.io.OutputStream;
26
 
import java.io.UnsupportedEncodingException;
27
 
import java.util.Set;
28
 
import java.util.HashSet;
29
 
import java.net.HttpURLConnection;
30
 
import java.net.MalformedURLException;
31
 
import java.net.ProtocolException;
32
 
import java.net.URL;
33
 
 
34
 
/**
35
 
 * A simple utility class for posting raw updates to a Solr server, 
36
 
 * has a main method so it can be run on the command line.
37
 
 * 
38
 
 */
39
 
public class SimplePostTool {
40
 
  public static final String DEFAULT_POST_URL = "http://localhost:8983/solr/update";
41
 
  public static final String VERSION_OF_THIS_TOOL = "1.4";
42
 
 
43
 
  private static final String DEFAULT_COMMIT = "yes";
44
 
  private static final String DEFAULT_OPTIMIZE = "no";
45
 
  private static final String DEFAULT_OUT = "no";
46
 
 
47
 
  private static final String DEFAULT_DATA_TYPE = "application/xml";
48
 
 
49
 
  private static final String DATA_MODE_FILES = "files";
50
 
  private static final String DATA_MODE_ARGS = "args";
51
 
  private static final String DATA_MODE_STDIN = "stdin";
52
 
  private static final String DEFAULT_DATA_MODE = DATA_MODE_FILES;
53
 
 
54
 
  private static final Set<String> DATA_MODES = new HashSet<String>();
55
 
  static {
56
 
    DATA_MODES.add(DATA_MODE_FILES);
57
 
    DATA_MODES.add(DATA_MODE_ARGS);
58
 
    DATA_MODES.add(DATA_MODE_STDIN);
59
 
  }
60
 
 
61
 
  protected URL solrUrl;
62
 
 
63
 
  public static void main(String[] args) {
64
 
    info("version " + VERSION_OF_THIS_TOOL);
65
 
 
66
 
    if (0 < args.length && ("-help".equals(args[0]) || "--help".equals(args[0]) || "-h".equals(args[0]))) {
67
 
      System.out.println
68
 
        ("This is a simple command line tool for POSTing raw data to a Solr\n"+
69
 
         "port.  Data can be read from files specified as commandline args,\n"+
70
 
         "as raw commandline arg strings, or via STDIN.\n"+
71
 
         "Examples:\n"+
72
 
         "  java -jar post.jar *.xml\n"+
73
 
         "  java -Ddata=args  -jar post.jar '<delete><id>42</id></delete>'\n"+
74
 
         "  java -Ddata=stdin -jar post.jar < hd.xml\n"+
75
 
         "  java -Durl=http://localhost:8983/solr/update/csv -Dtype=text/csv -jar post.jar *.csv\n"+
76
 
         "  java -Durl=http://localhost:8983/solr/update/json -Dtype=application/json -jar post.jar *.json\n"+
77
 
         "  java -Durl=http://localhost:8983/solr/update/extract?literal.id=a -Dtype=application/pdf -jar post.jar a.pdf\n"+
78
 
         "Other options controlled by System Properties include the Solr\n"+
79
 
         "URL to POST to, the Content-Type of the data, whether a commit\n"+
80
 
         "or optimize should be executed, and whether the response should\n"+
81
 
         "be written to STDOUT. These are the defaults for all System Properties:\n"+
82
 
         "  -Ddata=" + DEFAULT_DATA_MODE + "\n"+
83
 
         "  -Dtype=" + DEFAULT_DATA_TYPE + "\n"+
84
 
         "  -Durl=" + DEFAULT_POST_URL + "\n"+
85
 
         "  -Dcommit=" + DEFAULT_COMMIT + "\n"+
86
 
         "  -Doptimize=" + DEFAULT_OPTIMIZE + "\n"+
87
 
         "  -Dout=" + DEFAULT_OUT + "\n");
88
 
      return;
89
 
    }
90
 
 
91
 
    OutputStream out = null;
92
 
 
93
 
    URL u = null;
94
 
    try {
95
 
      u = new URL(System.getProperty("url", DEFAULT_POST_URL));
96
 
    } catch (MalformedURLException e) {
97
 
      fatal("System Property 'url' is not a valid URL: " + u);
98
 
    }
99
 
    final SimplePostTool t = new SimplePostTool(u);
100
 
 
101
 
    final String mode = System.getProperty("data", DEFAULT_DATA_MODE);
102
 
    if (! DATA_MODES.contains(mode)) {
103
 
      fatal("System Property 'data' is not valid for this tool: " + mode);
104
 
    }
105
 
 
106
 
    if ("yes".equals(System.getProperty("out", DEFAULT_OUT))) {
107
 
      out = System.out;
108
 
    }
109
 
 
110
 
    try {
111
 
      if (DATA_MODE_FILES.equals(mode)) {
112
 
        if (0 < args.length) {
113
 
          info("POSTing files to " + u + "..");
114
 
          t.postFiles(args, 0, out);
115
 
        } else {
116
 
          info("No files specified. (Use -h for help)");
117
 
        }
118
 
        
119
 
      } else if (DATA_MODE_ARGS.equals(mode)) {
120
 
        if (0 < args.length) {
121
 
          info("POSTing args to " + u + "..");
122
 
          for (String a : args) {
123
 
            t.postData(SimplePostTool.stringToStream(a), null, out);
124
 
          }
125
 
        }
126
 
        
127
 
      } else if (DATA_MODE_STDIN.equals(mode)) {
128
 
        info("POSTing stdin to " + u + "..");
129
 
        t.postData(System.in, null, out);
130
 
      }
131
 
      if ("yes".equals(System.getProperty("commit",DEFAULT_COMMIT))) {
132
 
        info("COMMITting Solr index changes..");
133
 
        t.commit();
134
 
      }
135
 
      if ("yes".equals(System.getProperty("optimize",DEFAULT_OPTIMIZE))) {
136
 
        info("Performing an OPTIMIZE..");
137
 
        t.optimize();
138
 
      }
139
 
    
140
 
    } catch(RuntimeException e) {
141
 
      e.printStackTrace();
142
 
      fatal("RuntimeException " + e);
143
 
    }
144
 
  }
145
 
 
146
 
  /** Post all filenames provided in args, return the number of files posted*/
147
 
  int postFiles(String [] args,int startIndexInArgs, OutputStream out) {
148
 
    int filesPosted = 0;
149
 
    for (int j = startIndexInArgs; j < args.length; j++) {
150
 
      File srcFile = new File(args[j]);
151
 
      if (srcFile.canRead()) {
152
 
        info("POSTing file " + srcFile.getName());
153
 
        postFile(srcFile, out);
154
 
        filesPosted++;
155
 
      } else {
156
 
        warn("Cannot read input file: " + srcFile);
157
 
      }
158
 
    }
159
 
    return filesPosted;
160
 
  }
161
 
  
162
 
  static void warn(String msg) {
163
 
    System.err.println("SimplePostTool: WARNING: " + msg);
164
 
  }
165
 
 
166
 
  static void info(String msg) {
167
 
    System.out.println("SimplePostTool: " + msg);
168
 
  }
169
 
 
170
 
  static void fatal(String msg) {
171
 
    System.err.println("SimplePostTool: FATAL: " + msg);
172
 
    System.exit(1);
173
 
  }
174
 
 
175
 
  /**
176
 
   * Constructs an instance for posting data to the specified Solr URL 
177
 
   * (ie: "http://localhost:8983/solr/update")
178
 
   */
179
 
  public SimplePostTool(URL solrUrl) {
180
 
    this.solrUrl = solrUrl;
181
 
  }
182
 
 
183
 
  /**
184
 
   * Does a simple commit operation 
185
 
   */
186
 
  public void commit() {
187
 
    doGet(appendParam(solrUrl.toString(), "commit=true"));
188
 
  }
189
 
 
190
 
  /**
191
 
   * Does a simple optimize operation 
192
 
   */
193
 
  public void optimize() {
194
 
    doGet(appendParam(solrUrl.toString(), "optimize=true"));
195
 
  }
196
 
 
197
 
  private String appendParam(String url, String param) {
198
 
    return url + (url.indexOf('?')>0 ? "&" : "?") + param;
199
 
  }
200
 
 
201
 
  /**
202
 
   * Opens the file and posts it's contents to the solrUrl,
203
 
   * writes to response to output.
204
 
   * @throws UnsupportedEncodingException 
205
 
   */
206
 
  public void postFile(File file, OutputStream output) {
207
 
 
208
 
    InputStream is = null;
209
 
    try {
210
 
      is = new FileInputStream(file);
211
 
      postData(is, (int)file.length(), output);
212
 
    } catch (IOException e) {
213
 
      fatal("Can't open/read file: " + file);
214
 
    } finally {
215
 
      try {
216
 
        if(is!=null) is.close();
217
 
      } catch (IOException e) {
218
 
        fatal("IOException while closing file: "+ e);
219
 
      }
220
 
    }
221
 
  }
222
 
 
223
 
  /**
224
 
   * Performs a simple get on the given URL
225
 
   * @param url
226
 
   */
227
 
  public void doGet(String url) {
228
 
    try {
229
 
      doGet(new URL(url));
230
 
    } catch (MalformedURLException e) {
231
 
      fatal("The specified URL "+url+" is not a valid URL. Please check");
232
 
    }
233
 
  }
234
 
  
235
 
  /**
236
 
   * Performs a simple get on the given URL
237
 
   * @param url
238
 
   */
239
 
  public void doGet(URL url) {
240
 
    try {
241
 
      HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
242
 
      if (HttpURLConnection.HTTP_OK != urlc.getResponseCode()) {
243
 
        fatal("Solr returned an error #" + urlc.getResponseCode() + 
244
 
            " " + urlc.getResponseMessage());
245
 
      }
246
 
    } catch (IOException e) {
247
 
      fatal("An error occured posting data to "+url+". Please check that Solr is running.");
248
 
    }
249
 
  }
250
 
 
251
 
  /**
252
 
   * Reads data from the data stream and posts it to solr,
253
 
   * writes to the response to output
254
 
   */
255
 
  public void postData(InputStream data, Integer length, OutputStream output) {
256
 
 
257
 
    final String type = System.getProperty("type", DEFAULT_DATA_TYPE);
258
 
 
259
 
    HttpURLConnection urlc = null;
260
 
    try {
261
 
      try {
262
 
        urlc = (HttpURLConnection) solrUrl.openConnection();
263
 
        try {
264
 
          urlc.setRequestMethod("POST");
265
 
        } catch (ProtocolException e) {
266
 
          fatal("Shouldn't happen: HttpURLConnection doesn't support POST??"+e);
267
 
                
268
 
        }
269
 
        urlc.setDoOutput(true);
270
 
        urlc.setDoInput(true);
271
 
        urlc.setUseCaches(false);
272
 
        urlc.setAllowUserInteraction(false);
273
 
        urlc.setRequestProperty("Content-type", type);
274
 
 
275
 
        if (null != length) urlc.setFixedLengthStreamingMode(length);
276
 
 
277
 
      } catch (IOException e) {
278
 
        fatal("Connection error (is Solr running at " + solrUrl + " ?): " + e);
279
 
      }
280
 
      
281
 
      OutputStream out = null;
282
 
      try {
283
 
        out = urlc.getOutputStream();
284
 
        pipe(data, out);
285
 
      } catch (IOException e) {
286
 
        fatal("IOException while posting data: " + e);
287
 
      } finally {
288
 
        try { if(out!=null) out.close(); } catch (IOException x) { /*NOOP*/ }
289
 
      }
290
 
      
291
 
      InputStream in = null;
292
 
      try {
293
 
        if (HttpURLConnection.HTTP_OK != urlc.getResponseCode()) {
294
 
          fatal("Solr returned an error #" + urlc.getResponseCode() + 
295
 
                " " + urlc.getResponseMessage());
296
 
        }
297
 
 
298
 
        in = urlc.getInputStream();
299
 
        pipe(in, output);
300
 
      } catch (IOException e) {
301
 
        fatal("IOException while reading response: " + e);
302
 
      } finally {
303
 
        try { if(in!=null) in.close(); } catch (IOException x) { /*NOOP*/ }
304
 
      }
305
 
      
306
 
    } finally {
307
 
      if(urlc!=null) urlc.disconnect();
308
 
    }
309
 
  }
310
 
 
311
 
  private static InputStream stringToStream(String s) {
312
 
    InputStream is = null;
313
 
    try {
314
 
      is = new ByteArrayInputStream(s.getBytes("UTF-8"));
315
 
    } catch (UnsupportedEncodingException e) {
316
 
      fatal("Shouldn't happen: UTF-8 not supported?!?!?!");
317
 
    }
318
 
    return is;
319
 
  }
320
 
 
321
 
  /**
322
 
   * Pipes everything from the source to the dest.  If dest is null, 
323
 
   * then everything is read fro msource and thrown away.
324
 
   */
325
 
  private static void pipe(InputStream source, OutputStream dest) throws IOException {
326
 
    byte[] buf = new byte[1024];
327
 
    int read = 0;
328
 
    while ( (read = source.read(buf) ) >= 0) {
329
 
      if (null != dest) dest.write(buf, 0, read);
330
 
    }
331
 
    if (null != dest) dest.flush();
332
 
  }
333
 
}