Using Java Processors

The App Studio Workflow module provides out of the box processors to manipulate queries and responses from the underlying search engine, as well as an SDK to write your own Java processors.

Implementing processors

You need to make some configuration changes to set up processors on your platforms. There are two ways, the simplest is directly on the platform config file, when the processor requires no arguments. For more complex processors that take input arguments there are additional steps.

Query Processor

Example: Add the user name to the query filtering on the ‘username’ field. Get the username either from the query payload itself or from the security provider.

src/main/conf/platforms/fusion/data.conf →

query-profile: my-profile
workflow: twigkit.client.processor.query.AddUser
...

src/main/java/twigkit/client/processor/query/AddUser.java →

package twigkit.client.processor.query;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import twigkit.model.Filter;
import twigkit.model.Query;
import twigkit.model.Value;
import twigkit.processor.QueryProcessor;
import twigkit.security.SecurityProvider;
public class AddUser extends QueryProcessor {
  private static final Logger logger = LoggerFactory.getLogger(AddUser.class);
  @Inject
  private SecurityProvider securityProvider;
  @Override
  public Query change(Query payload) {
      if (payload != null) {
          Filter userFilter = new Filter();
          userFilter.setField("user");
          Value user = new Value();
          if (securityProvider != null) {
              if (securityProvider.getUser() != null) {
                  user.setActual(securityProvider.getUser().getName());
                  userFilter.setValue(user);
                  payload.addFilter(userFilter);
              } else {
                  logger.error("No user retrieved from " + securityProvider.getClass().getName());
              }
          } else if (payload.getUser() != null) {
              user.setActual(payload.getUser().getName());
              payload.addFilter(userFilter);
          } else {
              logger.error("No SecurityProvider provided and no user on the query!");
          }
      }
      return payload;
  }
}

Response processor with configuration

Example: If the response has no results send the query again to a different platform.

src/main/conf/platforms/fusion/fusion.conf →

requestHandler: /select
query-profile: my-profile
workflow: processors.response.fuzzy-search

src/main/conf/platforms/fusion/fuzzy.conf →

requestHandler: /spell

src/main/conf/processors/response/fuzzy-search.conf →

name: twigkit.client.processor.response.FuzzySearch
fuzzy-platform: platforms.fusion.fuzzy

src/main/java/twigkit/client/processor/response/FuzzySearch.java

package twigkit.client.processor.response;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import twigkit.conf.ConfiguredPlatformProvider;
import twigkit.model.Query;
import twigkit.model.Response;
import twigkit.model.messages.Message;
import twigkit.platform.Platform;
import twigkit.processor.ResponseProcessor;
import java.util.ArrayList;
import java.util.List;
public class FuzzySearch extends ResponseProcessor {
  private static final Logger logger = LoggerFactory.getLogger(FuzzySearch.class);
  @Inject
  private ConfiguredPlatformProvider platformProvider;
  @Override
  public Response change(Response response) {
      String fuzzyPlatformId = getParameterStringValue("fuzzy-platform");
      Platform fuzzyPlatform = platformProvider.get(fuzzyPlatformId);
      if (response.getHits().getActual() > 0){
          return response;
      } else {
          Query query = response.getQuery();
          Response fuzzyResponse = fuzzyPlatform.search(query);
          String queryString = query.getValue().getActualAsString();
          Message fuzzyMessage = new Message();
          fuzzyMessage.setType("fuzzy-results");
          fuzzyMessage.setValue("No results found for the term <em>"+queryString+"</em>. Returning results with similar words.");
          List<Message> messages = new ArrayList<Message>();
          messages.add(fuzzyMessage);
          fuzzyResponse.setMessages(messages);
          return fuzzyResponse;
      }
  }
}