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

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/servlet/DirectSolrConnection.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
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.servlet;
19
 
 
20
 
import java.io.File;
21
 
import java.io.StringWriter;
22
 
import java.util.ArrayList;
23
 
import java.util.HashMap;
24
 
import java.util.List;
25
 
 
26
 
import org.apache.solr.common.SolrException;
27
 
import org.apache.solr.common.params.CommonParams;
28
 
import org.apache.solr.common.params.MapSolrParams;
29
 
import org.apache.solr.common.params.SolrParams;
30
 
import org.apache.solr.common.util.ContentStream;
31
 
import org.apache.solr.common.util.ContentStreamBase;
32
 
import org.apache.solr.core.CoreContainer;
33
 
import org.apache.solr.core.CoreDescriptor;
34
 
import org.apache.solr.core.SolrConfig;
35
 
import org.apache.solr.core.SolrCore;
36
 
import org.apache.solr.core.SolrResourceLoader;
37
 
import org.apache.solr.request.SolrQueryRequest;
38
 
import org.apache.solr.request.SolrRequestHandler;
39
 
import org.apache.solr.response.QueryResponseWriter;
40
 
import org.apache.solr.response.SolrQueryResponse;
41
 
import org.apache.solr.schema.IndexSchema;
42
 
 
43
 
/**
44
 
 * DirectSolrConnection provides an interface to Solr that is similar to
45
 
 * the the HTTP interface, but does not require an HTTP connection.
46
 
 * 
47
 
 * This class is designed to be as simple as possible and allow for more flexibility
48
 
 * in how you interface to Solr.
49
 
 * 
50
 
 * @version $Id: DirectSolrConnection.java 1128442 2011-05-27 20:15:59Z mikemccand $
51
 
 * @since solr 1.2
52
 
 */
53
 
public class DirectSolrConnection 
54
 
{
55
 
  final SolrCore core;
56
 
  final SolrRequestParsers parser;
57
 
  
58
 
  /**
59
 
   * Initialize using the static singleton SolrCore.getSolrCore().
60
 
   * 
61
 
   * @deprecated use {@link #DirectSolrConnection(SolrCore)}
62
 
   */
63
 
  @Deprecated
64
 
  public DirectSolrConnection()
65
 
  {
66
 
    this( SolrCore.getSolrCore() );
67
 
  }
68
 
 
69
 
  /**
70
 
   * Initialize using an explicit SolrCore
71
 
   */
72
 
  public DirectSolrConnection( SolrCore c )
73
 
  {
74
 
    core = c;
75
 
    parser = new SolrRequestParsers( c.getSolrConfig() );
76
 
  }
77
 
 
78
 
  /**
79
 
   * This constructor is designed to make it easy for JNI embedded applications 
80
 
   * to setup the entire solr environment with a simple interface.  It takes three parameters:
81
 
   * 
82
 
   * <code>instanceDir:</code> The solr instance directory.  If null, it will check the standard 
83
 
   * places first (JNDI,properties,"solr" directory)
84
 
   * 
85
 
   * <code>dataDir:</code> where the index is stored. 
86
 
   * 
87
 
   * <code>loggingPath:</code> Path to a java.util.logging.config.file.  If the path represents
88
 
   * an absolute path or is relative to the CWD, it will use that.  Next it will try a path 
89
 
   * relative to the instanceDir.  If none of these files exist, it will error.
90
 
   */
91
 
  public DirectSolrConnection( String instanceDir, String dataDir, String loggingPath )
92
 
  {
93
 
    // If a loggingPath is specified, try using that (this needs to happen first)
94
 
    if( loggingPath != null ) {
95
 
      File loggingConfig = new File( loggingPath );
96
 
      if( !loggingConfig.exists() && instanceDir != null ) {
97
 
        loggingConfig = new File( new File(instanceDir), loggingPath  );
98
 
      }
99
 
      if( loggingConfig.exists() ) {
100
 
        System.setProperty("java.util.logging.config.file", loggingConfig.getAbsolutePath() ); 
101
 
      }
102
 
      else {
103
 
        throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "can not find logging file: "+loggingConfig );
104
 
      }
105
 
    }
106
 
    
107
 
    if( instanceDir == null ) {
108
 
      instanceDir = SolrResourceLoader.locateInstanceDir();
109
 
    }
110
 
    
111
 
    // Initialize 
112
 
    try {
113
 
      CoreContainer cores = new CoreContainer(new SolrResourceLoader(instanceDir));
114
 
      SolrConfig solrConfig = new SolrConfig(instanceDir, SolrConfig.DEFAULT_CONF_FILE, null);
115
 
      CoreDescriptor dcore = new CoreDescriptor(cores, "", solrConfig.getResourceLoader().getInstanceDir());
116
 
      IndexSchema indexSchema = new IndexSchema(solrConfig, instanceDir+"/conf/schema.xml", null);
117
 
      core = new SolrCore( null, dataDir, solrConfig, indexSchema, dcore);
118
 
      cores.register("", core, false);
119
 
      parser = new SolrRequestParsers( solrConfig );
120
 
    } 
121
 
    catch (Exception ee) {
122
 
      throw new RuntimeException(ee);
123
 
    }
124
 
  }
125
 
  
126
 
 
127
 
  /**
128
 
   * For example:
129
 
   * 
130
 
   * String json = solr.request( "/select?qt=dismax&wt=json&q=...", null );
131
 
   * String xml = solr.request( "/update", "&lt;add><doc><field ..." );
132
 
   */
133
 
  public String request( String pathAndParams, String body ) throws Exception
134
 
  {
135
 
    String path = null;
136
 
    SolrParams params = null;
137
 
    int idx = pathAndParams.indexOf( '?' );
138
 
    if( idx > 0 ) {
139
 
      path = pathAndParams.substring( 0, idx );
140
 
      params = SolrRequestParsers.parseQueryString( pathAndParams.substring(idx+1) );
141
 
    }
142
 
    else {
143
 
      path= pathAndParams;
144
 
      params = new MapSolrParams( new HashMap<String, String>() );
145
 
    }
146
 
    
147
 
    // Extract the handler from the path or params
148
 
    SolrRequestHandler handler = core.getRequestHandler( path );
149
 
    if( handler == null ) {
150
 
      if( "/select".equals( path ) || "/select/".equalsIgnoreCase( path) ) {
151
 
        String qt = params.get( CommonParams.QT );
152
 
        handler = core.getRequestHandler( qt );
153
 
        if( handler == null ) {
154
 
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "unknown handler: "+qt);
155
 
        }
156
 
      }
157
 
    }
158
 
 
159
 
    if( handler == null ) {
160
 
      throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "unknown handler: "+path );
161
 
    }
162
 
 
163
 
    return request(handler, params, body);
164
 
  }
165
 
 
166
 
  public String request(SolrRequestHandler handler, SolrParams params, String body ) throws Exception {
167
 
 
168
 
    if (params == null)
169
 
      params = new MapSolrParams( new HashMap<String, String>() );
170
 
 
171
 
    // Make a stream for the 'body' content
172
 
    List<ContentStream> streams = new ArrayList<ContentStream>( 1 );
173
 
    if( body != null && body.length() > 0 ) {
174
 
      streams.add( new ContentStreamBase.StringStream( body ) );
175
 
    }
176
 
 
177
 
    SolrQueryRequest req = null;
178
 
    try {
179
 
      req = parser.buildRequestFrom( core, params, streams );
180
 
      SolrQueryResponse rsp = new SolrQueryResponse();
181
 
      core.execute( handler, req, rsp );
182
 
      if( rsp.getException() != null ) {
183
 
        throw rsp.getException();
184
 
      }
185
 
 
186
 
      // Now write it out
187
 
      QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
188
 
      StringWriter out = new StringWriter();
189
 
      responseWriter.write(out, req, rsp);
190
 
      return out.toString();
191
 
    } finally {
192
 
      if (req != null) {
193
 
        req.close();
194
 
      }
195
 
    }
196
 
  }
197
 
  
198
 
  /**
199
 
   * Use this method to close the underlying SolrCore.
200
 
   * 
201
 
   * @since solr 1.3
202
 
   */
203
 
  public void close() {
204
 
    core.close();
205
 
  }
206
 
}