blob: fbe544a42ad7a804dfd7e5b2c7417b9e7fd0362d [file] [log] [blame]
package org.apache.solr.rest.schema;
/*
* 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.
*/
import com.google.inject.Inject;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.request.SolrQueryRequestDecoder;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* This class responds to requests at /solr/(corename)/schema/fieldtype/(typename)
* where "typename" is the name of a field type in the schema.
* <p/>
* The GET method returns properties for the named field type.
*/
public class FieldTypeSR extends BaseFieldTypeResource implements FieldTypeResource {
private static final Logger log = LoggerFactory.getLogger(FieldTypeSR.class);
private String typeName;
@Inject
public FieldTypeSR(SolrQueryRequestDecoder requestDecoder) {
super(requestDecoder);
}
@Override
public void doInit() throws ResourceException {
super.doInit();
if (isExisting()) {
typeName = (String) getRequestAttributes().get(IndexSchema.NAME);
try {
typeName = null == typeName ? "" : urlDecode(typeName.trim()).trim();
} catch (UnsupportedEncodingException e) {
throw new ResourceException(e);
}
}
}
@Override
public Representation get() {
try {
if (typeName.isEmpty()) {
final String message = "Field type name is missing";
throw new SolrException(ErrorCode.BAD_REQUEST, message);
} else {
FieldType fieldType = getSchema().getFieldTypes().get(typeName);
if (null == fieldType) {
final String message = "Field type '" + typeName + "' not found.";
throw new SolrException(ErrorCode.NOT_FOUND, message);
}
getSolrResponse().add(IndexSchema.FIELD_TYPE, getFieldTypeProperties(fieldType));
}
} catch (Exception e) {
getSolrResponse().setException(e);
}
handlePostExecution(log);
return new SolrOutputRepresentation();
}
/**
* Returns a field list using the given field type by iterating over all fields
* defined in the schema.
*/
@Override
protected List<String> getFieldsWithFieldType(FieldType fieldType) {
List<String> fields = new ArrayList<String>();
for (SchemaField schemaField : getSchema().getFields().values()) {
if (schemaField.getType().getTypeName().equals(fieldType.getTypeName())) {
fields.add(schemaField.getName());
}
}
Collections.sort(fields);
return fields;
}
/**
* Returns a dynamic field list using the given field type by iterating over all
* dynamic fields defined in the schema.
*/
@Override
protected List<String> getDynamicFieldsWithFieldType(FieldType fieldType) {
List<String> dynamicFields = new ArrayList<String>();
for (SchemaField prototype : getSchema().getDynamicFieldPrototypes()) {
if (prototype.getType().getTypeName().equals(fieldType.getTypeName())) {
dynamicFields.add(prototype.getName());
}
}
return dynamicFields; // Don't sort these - they're matched in order
}
}