Fixed a bug that prevented fetching Data of players with non-lowercase

names. Also added an exception to show an error message when
non-existing players are requested.
This commit is contained in:
Pascal Koenig
2011-09-24 23:53:21 +02:00
parent 7a40761e00
commit 48fea3f1a5
5 changed files with 80 additions and 44 deletions

View File

@@ -16,6 +16,9 @@ package de.sockenklaus.XmlStats.Datasource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import de.sockenklaus.XmlStats.Exceptions.UserNotFoundException;
// TODO: Auto-generated Javadoc
/**
@@ -28,7 +31,7 @@ public abstract class Datasource {
*
* @return the array list
*/
public ArrayList<String> fetchAllPlayers(){
public static ArrayList<String> fetchAllPlayers(){
File[] files = new File("world/players").listFiles();
ArrayList<String> result = new ArrayList<String>();
@@ -44,4 +47,18 @@ public abstract class Datasource {
return result;
}
public static boolean userExists(String player){
return fetchAllPlayers().contains(player);
}
public static List<String> fetchValidUsers(List<String> list) throws UserNotFoundException{
ArrayList<String> output = new ArrayList<String>();
for (String possibleUser : list){
if(Datasource.userExists(possibleUser)) output.add(possibleUser);
}
if(output.isEmpty()) throw new UserNotFoundException("No valid user has been found!");
else return output;
}
}

View File

@@ -14,20 +14,9 @@
*/
package de.sockenklaus.XmlStats.Datasource;
import java.util.ArrayList;
// TODO: Auto-generated Javadoc
/**
* The Class UsersDS.
*/
public class UsersDS extends Datasource {
/**
* Gets the all players.
*
* @return the all players
*/
public ArrayList<String> getAllPlayers(){
return fetchAllPlayers();
}
}
}

View File

@@ -0,0 +1,19 @@
/**
*
*/
package de.sockenklaus.XmlStats.Exceptions;
/**
* @author socrates
*
*/
public class UserNotFoundException extends Exception {
/**
*
*/
private static final long serialVersionUID = -6660078004710596491L;
public UserNotFoundException(String s){
super(s);
}
}

View File

@@ -23,7 +23,7 @@ import org.w3c.dom.Element;
import de.sockenklaus.XmlStats.XmlStats;
import de.sockenklaus.XmlStats.XmlStatsRegistry;
import de.sockenklaus.XmlStats.Datasource.UsersDS;
import de.sockenklaus.XmlStats.Datasource.Datasource;
// TODO: Auto-generated Javadoc
/**
@@ -36,10 +36,9 @@ public class UserList extends XmlWorker {
*/
@Override
public Element getXml(Map<String, List<String>> parameters) {
UsersDS users = new UsersDS();
Element elem_users = this.doc.createElement("users");
elem_users.setAttribute("count", String.valueOf(users.getAllPlayers().size()));
elem_users.setAttribute("count", String.valueOf(Datasource.fetchAllPlayers().size()));
/*
* Get list online player names
@@ -62,7 +61,7 @@ public class UserList extends XmlWorker {
* Hier wird das XML aufgebaut
*/
for(String playerName : users.getAllPlayers()){
for(String playerName : Datasource.fetchAllPlayers()){
Element elem_player = this.doc.createElement("user");
elem_player.appendChild(getTextElem("name", playerName));

View File

@@ -46,7 +46,7 @@ import com.sun.net.httpserver.HttpExchange;
import de.sockenklaus.XmlStats.XmlStats;
import de.sockenklaus.XmlStats.Datasource.Datasource;
import de.sockenklaus.XmlStats.Datasource.UsersDS;
import de.sockenklaus.XmlStats.Exceptions.UserNotFoundException;
// TODO: Auto-generated Javadoc
/**
@@ -78,24 +78,38 @@ public abstract class XmlWorker implements HttpHandler {
/*
* Parse the parameters
*/
this.factory = DocumentBuilderFactory.newInstance();
try {
this.builder = this.factory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
this.doc = this.builder.newDocument();
try {
this.source = new DOMSource(this.doc);
this.writer = new StringWriter();
this.result = new StreamResult(this.writer);
this.tf = TransformerFactory.newInstance();
this.transformer = this.tf.newTransformer();
} catch (TransformerConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Element root = this.doc.createElement("xmlstats");
List<String> userList;
this.doc.appendChild(root);
try {
parameters = parseParameters(queryString);
/*
* Create the XML doc stuff....
*/
this.factory = DocumentBuilderFactory.newInstance();
this.builder = this.factory.newDocumentBuilder();
this.doc = this.builder.newDocument();
this.source = new DOMSource(this.doc);
this.writer = new StringWriter();
this.result = new StreamResult(this.writer);
this.tf = TransformerFactory.newInstance();
this.transformer = this.tf.newTransformer();
Datasource ds = new UsersDS();
Element root = this.doc.createElement("xmlstats");
List<String> playerList;
this.doc.appendChild(root);
/*
* Actually create the XML
*/
@@ -106,33 +120,32 @@ public abstract class XmlWorker implements HttpHandler {
else if(parameters.containsKey("user")){
if (parameters.get("user").contains("*")){
playerList = ds.fetchAllPlayers();
userList = Datasource.fetchAllPlayers();
}
else {
playerList = parameters.get("user");
userList = Datasource.fetchValidUsers(parameters.get("user"));
}
root.appendChild(getUserXml(playerList, parameters));
root.appendChild(getUserXml(userList, parameters));
}
if(parameters.containsKey("sum")){
if(parameters.get("sum").contains("*")){
playerList = ds.fetchAllPlayers();
userList = Datasource.fetchAllPlayers();
}
else {
playerList = parameters.get("sum");
userList = Datasource.fetchValidUsers(parameters.get("sum"));
}
root.appendChild(getSumXml(playerList, parameters));
root.appendChild(getSumXml(userList, parameters));
}
/*
* Build string from XML
*/
}
catch(TransformerConfigurationException e){
}
catch(ParserConfigurationException e){
catch(UserNotFoundException e){
root.setAttribute("status", "error");
root.appendChild(getTextElem("error", e.getMessage()));
}
catch (UnsupportedEncodingException e) {
@@ -174,8 +187,6 @@ public abstract class XmlWorker implements HttpHandler {
exchange.getResponseBody().write(byteResponse);
}
catch(UnsupportedEncodingException e){
}
catch(IOException ex){
XmlStats.LogError("Fehler beim Senden der HTTP-Antwort.");
XmlStats.LogError(ex.getMessage());
@@ -235,12 +246,13 @@ public abstract class XmlWorker implements HttpHandler {
}
if(param.length > 1){
valueArr = URLDecoder.decode(param[1].toLowerCase(), System.getProperty("file.encoding")).split(",");
valueArr = URLDecoder.decode(param[1], System.getProperty("file.encoding")).split(",");
}
List<String> values = new ArrayList<String>();
for (String value : valueArr){
if (!values.contains(value)){
XmlStats.LogDebug("ParseParameters() found: "+key+" = "+value);
values.add(value);
}
}