/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.search.controller;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.perforce.search.configuration.Configuration;
import com.perforce.search.model.impl.ErrorModels;
@Controller
@RequestMapping("/api")
public class QueueController {
private static final Logger log = Logger.getLogger(QueueController.class);
private Configuration config;
private String queueLocation;
public QueueController(final Configuration config) {
this.config = config;
// read the token/queue location
this.queueLocation = config.getQueueDir();
File queue = new File(this.queueLocation);
if (!queue.exists()) {
if (!queue.mkdirs()) {
log.error("Failed to create queue location: " + queueLocation);
}
}
}
@RequestMapping(value = "/queue/reindex/{token}", method = RequestMethod.POST)
public ModelAndView reindexTask(final HttpServletRequest request,
final HttpServletResponse response,
@PathVariable(value = "token") final String token,
@RequestBody final String fileRev) {
// this is to request a possible reindexing of a single file
// possibly with a rev number.
return queueTask(request, response, config.getQueueToken(),
String.format("%s,%s", "reindex", fileRev));
}
@RequestMapping(value = "/queue/{token}", method = RequestMethod.POST)
public ModelAndView queueTask(final HttpServletRequest request,
final HttpServletResponse response,
@PathVariable(value = "token") final String token,
@RequestBody final String serverActionModelOrig) {
// do what swarm does:
// 1) validate {token} against configured token, reject if not equal
// 2) write a file out with the timestamp as the name, serverActionModel
// as the content
// just in case you dork your curl and do not put
// "--header "Content-Type: text/plain", try to decode it
String serverActionModel = serverActionModelOrig.replaceAll("%2C", ",")
.replaceAll("=", "");
log.debug("serveractionmodel = " + serverActionModel);
try {
if (!token.equals(config.getQueueToken())) {
log.error("Bad token for search index trigger: " + token);
return ResponseBuilder
.buildResponse(ErrorModels.Error_InvalidQueueToken);
}
String fname = queueLocation
+ String.valueOf(System.currentTimeMillis()) + ".";
File fout = null;
final int maxFnameRetries = 10;
for (int i = 0; i < maxFnameRetries; ++i) {
File file = new File(fname + String.valueOf(i));
if (!file.exists()) {
fout = file;
break;
}
}
if (fout == null) {
log.error("Failed to open new token file: " + fname);
}
fout.createNewFile();
FileWriter fw = new FileWriter(fout.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(serverActionModel);
bw.close();
} catch (IOException e) {
log.error(e);
return ResponseBuilder.buildResponse(ErrorModels.Error_Generic);
}
return ResponseBuilder.buildResponse(ErrorModels.Error_OK);
}
}