/*
Copyright 2012 Christian Prause and Fraunhofer FIT
Licensed 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.
*/
package net.sf.collabreview.mediawiki;
import net.sf.collabreview.core.CollabReview;
import net.sf.collabreview.core.CollabReviewSingleton;
import net.sf.collabreview.core.configuration.ConfigurationData;
import net.sf.collabreview.core.users.Author;
import net.sf.collabreview.web.ExtensionServlet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* Serves the level icon images for users.
* Call this servlet with an author name to let it serve an image data response.
*
* @author silviya
* @date 13.10.2009 15:18:11
*/
public class LevelIconServlet extends ExtensionServlet {
private static final Log logger = LogFactory.getLog(LevelIconServlet.class);
private CollabReview collabReview = CollabReviewSingleton.get();
private static final int[] levelScore = {
0, 1, 50, 150, 250,
500, 750, 1000, 1250, 1500,
1750, 2000, 2250, 2500, 3000};
private static final String[] levelName = {
"01_blinky", "02_knechtruprecht", "03_snowball", "04_krusty", "05_ned",
"06_judge", "07_homer", "08_bart", "09_maggie", "10_lisa",
"11_marge", "12_hibbert", "13_apu", "14_frink", "15_burns"};
static {
assert levelScore.length == levelName.length;
}
public LevelIconServlet() {
}
protected synchronized void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
logger.debug("Servicing web service call");
int nameLength = request.getParameter("name").length();
String userName = request.getParameter("name").substring(0, nameLength - 4);
Author author = collabReview.getAuthorManager().getAuthor(userName);
response.setContentType("image/gif");
String imageName;
imageName = "00_bonehead";
if (author != null) { //check if user name exists in database
Float userRespScore = collabReview.getReputationMetricManager().findReputationMetric("silfwiki").getAuthorScore(author.getName());
if (userRespScore != null) {
for (int i = 0; i < levelScore.length; i++) {
if (userRespScore >= levelScore[i]) {
imageName = levelName[i];
}
}
}
}
byte[] ba = getImageBytes(imageName);
try {
response.setContentLength(ba.length); //wie gross ist die auszulesende Datei
response.getOutputStream().write(ba);
}
catch (NullPointerException e) {
logger.error("Image not loaded: " + imageName, e);
}
}
private byte[] getImageBytes(String imageName) {
byte[] buf = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// file open, read, close
try {
InputStream fin = this.getClass().getClassLoader().getResourceAsStream("net/sf/collabreview/mediawiki/images/levels_simpsons/" + imageName + ".gif");
int n;
while ((n = fin.read(buf)) != -1) {
baos.write(buf, 0, n);
}
return baos.toByteArray();
}
catch (FileNotFoundException e) {
logger.error("Image not found " + e);
}
catch (IOException ioe) {
logger.error("Exception while reading the image " + ioe);
}
return new byte[0];
}
protected void configure(CollabReview collabReview, ConfigurationData config) {
}
protected void destroy() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
}
}