001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.servlet;
017:
018: import java.io.File;
019: import java.io.StringWriter;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023:
024: import org.apache.solr.core.Config;
025: import org.apache.solr.core.SolrConfig;
026: import org.apache.solr.core.SolrCore;
027: import org.apache.solr.core.SolrException;
028: import org.apache.solr.request.MapSolrParams;
029: import org.apache.solr.request.QueryResponseWriter;
030: import org.apache.solr.request.SolrParams;
031: import org.apache.solr.request.SolrQueryRequest;
032: import org.apache.solr.request.SolrQueryResponse;
033: import org.apache.solr.request.SolrRequestHandler;
034: import org.apache.solr.schema.IndexSchema;
035: import org.apache.solr.util.ContentStream;
036: import org.apache.solr.util.ContentStreamBase;
037:
038: /**
039: * DirectSolrConnection provides an interface to solr that is similar to
040: * the the HTTP interface, but does not require an HTTP connection.
041: *
042: * This class is designed to be as simple as possible and alow for more flexibility
043: * in how you interface to solr.
044: *
045: * @author ryan
046: * @version $Id: DirectSolrConnection.java 542679 2007-05-29 22:28:21Z ryan $
047: * @since solr 1.2
048: */
049: public class DirectSolrConnection {
050: final SolrCore core;
051: final SolrRequestParsers parser;
052:
053: /**
054: * Initialize using the static singleton SolrCore.getSolrCore().
055: */
056: public DirectSolrConnection() {
057: core = SolrCore.getSolrCore();
058: parser = new SolrRequestParsers(core, SolrConfig.config);
059: }
060:
061: /**
062: * Initialize using an explicit SolrCore
063: */
064: public DirectSolrConnection(SolrCore c) {
065: core = c;
066: parser = new SolrRequestParsers(core, SolrConfig.config);
067: }
068:
069: /**
070: * This constructor is designed to make it easy for JNI embedded applications
071: * to setup the entire solr environment with a simple interface. It takes three parameters:
072: *
073: * <code>instanceDir:</code> The solr instance directory. If null, it will check the standard
074: * places first (JNDI,properties,"solr" directory)
075: *
076: * <code>dataDir:</code> where the index is stored.
077: *
078: * <code>loggingPath:</code> Path to a java.util.logging.config.file. If the path represents
079: * an absolute path or is relative to the CWD, it will use that. Next it will try a path
080: * relative to the instanceDir. If none of these files exist, it will error.
081: */
082: public DirectSolrConnection(String instanceDir, String dataDir,
083: String loggingPath) {
084: // If a loggingPath is specified, try using that (this needs to happen first)
085: if (loggingPath != null) {
086: File loggingConfig = new File(loggingPath);
087: if (!loggingConfig.exists() && instanceDir != null) {
088: loggingConfig = new File(new File(instanceDir),
089: loggingPath);
090: }
091: if (loggingConfig.exists()) {
092: System.setProperty("java.util.logging.config.file",
093: loggingConfig.getAbsolutePath());
094: } else {
095: throw new SolrException(
096: SolrException.ErrorCode.SERVER_ERROR,
097: "can not find logging file: " + loggingConfig);
098: }
099: }
100:
101: // Set the instance directory
102: if (instanceDir != null) {
103: if (Config.isInstanceDirInitialized()) {
104: String dir = Config.getInstanceDir();
105: if (!dir.equals(instanceDir)) {
106: throw new SolrException(
107: SolrException.ErrorCode.SERVER_ERROR,
108: "already initalized: " + dir);
109: }
110: }
111: Config.setInstanceDir(instanceDir);
112: }
113:
114: // If the Data directory is specified, initalize SolrCore directly
115: if (dataDir != null) {
116: core = new SolrCore(dataDir, new IndexSchema(instanceDir
117: + "/conf/schema.xml"));
118: } else {
119: core = SolrCore.getSolrCore();
120: }
121: parser = new SolrRequestParsers(core, SolrConfig.config);
122: }
123:
124: /**
125: * For example:
126: *
127: * String json = solr.request( "/select?qt=dismax&wt=json&q=...", null );
128: * String xml = solr.request( "/update", "<add><doc><field ..." );
129: *
130: */
131: public String request(String pathAndParams, String body)
132: throws Exception {
133: String path = null;
134: SolrParams params = null;
135: int idx = pathAndParams.indexOf('?');
136: if (idx > 0) {
137: path = pathAndParams.substring(0, idx);
138: params = SolrRequestParsers.parseQueryString(pathAndParams
139: .substring(idx + 1));
140: } else {
141: path = pathAndParams;
142: params = new MapSolrParams(new HashMap<String, String>());
143: }
144:
145: // Extract the handler from the path or params
146: SolrRequestHandler handler = core.getRequestHandler(path);
147: if (handler == null) {
148: if ("/select".equals(path)
149: || "/select/".equalsIgnoreCase(path)) {
150: String qt = params.get(SolrParams.QT);
151: handler = core.getRequestHandler(qt);
152: if (handler == null) {
153: throw new SolrException(
154: SolrException.ErrorCode.BAD_REQUEST,
155: "unknown handler: " + qt);
156: }
157: }
158: }
159: if (handler == null) {
160: throw new SolrException(
161: SolrException.ErrorCode.BAD_REQUEST,
162: "unknown handler: " + path);
163: }
164:
165: // Make a stream for the 'body' content
166: List<ContentStream> streams = new ArrayList<ContentStream>(1);
167: if (body != null && body.length() > 0) {
168: streams.add(new ContentStreamBase.StringStream(body));
169: }
170:
171: SolrQueryRequest req = parser.buildRequestFrom(params, streams);
172: SolrQueryResponse rsp = new SolrQueryResponse();
173: core.execute(handler, req, rsp);
174: if (rsp.getException() != null) {
175: throw rsp.getException();
176: }
177:
178: // Now write it out
179: QueryResponseWriter responseWriter = core
180: .getQueryResponseWriter(req);
181: StringWriter out = new StringWriter();
182: responseWriter.write(out, req, rsp);
183: return out.toString();
184: }
185: }
|