Skip to content

Commit

Permalink
Merge branch 'fix/upload'
Browse files Browse the repository at this point in the history
  • Loading branch information
gab1one committed Aug 20, 2014
2 parents ab64ce6 + 06c4aad commit 1332733
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 302 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bin/
build/
log/
config/
.idea/
.project
.settings/
.gradle/
.classpath
.directory
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
theGroup = com.team-grit
theName = GRIT
theVersion = v1.0-
theName = GRadeIT
theVersion = v1.1-
theSourceCompatibility = 1.7
119 changes: 93 additions & 26 deletions src/webserver/EntityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -16,6 +17,10 @@
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
Expand All @@ -41,7 +46,7 @@ public abstract class EntityHandler extends AbstractHandler {
* A GSON instance for JSON parsing.
*/
protected static final Gson GSON = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation().create();
.excludeFieldsWithoutExposeAnnotation().create();

/**
* The grit-wide logger.
Expand Down Expand Up @@ -75,15 +80,15 @@ public EntityHandler() {

/*
* (non-Javadoc)
*
*
* @see org.eclipse.jetty.server.Handler#handle(java.lang.String,
* org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
@Override
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
public void handle(
String target, Request baseRequest, HttpServletRequest request,
HttpServletResponse response) throws IOException {

/*
* If this request contains a submitted file, use the multi-part
Expand All @@ -109,20 +114,20 @@ public void handle(String target, Request baseRequest,
*/
try {
response.getWriter().println(doAction(target, request));
} catch (BadRequestException e) {
String message = e.getMessage();
} catch (final BadRequestException e) {
final String message = e.getMessage();
LOGGER.severe(message);
response.setContentType("text/plain;charset=utf-8");
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().println(message);
} catch (InternalActionErrorException e) {
String message = e.getMessage();
} catch (final InternalActionErrorException e) {
final String message = e.getMessage();
LOGGER.severe(message);
response.setContentType("text/plain;charset=utf-8");
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
response.getWriter().println(message);
} catch (Exception e) {
String message =
} catch (final Exception e) {
final String message =
"An unexpected error occured:\n"
+ e.getClass().getSimpleName() + ":\n"
+ e.getMessage();
Expand Down Expand Up @@ -154,9 +159,9 @@ public void handle(String target, Request baseRequest,
* @throws InternalActionErrorException
* if something else goes wrong (e.g. could not write to file)
*/
protected abstract String doAction(String target,
HttpServletRequest request) throws BadRequestException,
InternalActionErrorException;
protected abstract String doAction(
String target, HttpServletRequest request)
throws BadRequestException, InternalActionErrorException;

// --------------------- PARAMETER HELPERS ---------------------

Expand All @@ -178,7 +183,7 @@ protected String parseName(String parameter, String nameType)
throw new BadRequestException("The passed " + nameType
+ " is null.");
}
String name = parameter.trim();
final String name = parameter.trim();
if (!name.matches(".+")) {
throw new BadRequestException("The passed " + nameType
+ " is invalid.");
Expand Down Expand Up @@ -207,7 +212,7 @@ protected LanguageType parseLanguageType(String parameter)
}
try {
return LanguageType.valueOf(parameter);
} catch (NullPointerException e) {
} catch (final NullPointerException e) {
throw new BadRequestException(
"The passed language type does not exist.");
}
Expand All @@ -234,7 +239,7 @@ protected ConnectionType parseConnectionType(String parameter)
}
try {
return ConnectionType.valueOf(parameter);
} catch (NullPointerException e) {
} catch (final NullPointerException e) {
throw new BadRequestException(
"The passed connection type does not exist.");
}
Expand All @@ -255,7 +260,7 @@ protected String parseLocation(String parameter)
if (parameter == null) {
throw new BadRequestException("The passed location is null.");
}
String location = parameter.trim();
final String location = parameter.trim();
if (!location.matches(".+")) {
throw new BadRequestException("The passed location is invalid.");
}
Expand All @@ -279,7 +284,7 @@ protected Date parseDateTime(String parameter) throws BadRequestException {
}
try {
return DATE_TIME_FORMAT.parse(parameter);
} catch (ParseException e) {
} catch (final ParseException e) {
throw new BadRequestException(e.getMessage()
+ "\nA date has to be formatted like this: \""
+ DATE_TIME_FORMAT.toPattern() + "\".");
Expand Down Expand Up @@ -307,8 +312,8 @@ protected long parseTimePeriod(String parameter)
+ "A time has to be formatted like this: \""
+ TIME_FORMAT.toPattern() + "\".");
}
String[] time = parameter.split(":");
long period =
final String[] time = parameter.split(":");
final long period =
((Integer.parseInt(time[0]) * 60) + Integer.parseInt(time[1])) * 60 * 1000;
return period;
}
Expand Down Expand Up @@ -399,15 +404,43 @@ protected int parseId(String parameter, String entityName)
*/
protected void writeSubmittedFile(Part part, Path outputDirectory)
throws BadRequestException, InternalActionErrorException {
String submittedFileName = part.getSubmittedFileName();
final String submittedFileName = part.getSubmittedFileName();
if ((submittedFileName == null) || ("".equals(submittedFileName))) {
throw new BadRequestException("No file submitted!");
}
try {
Path outFilePath = outputDirectory.resolve(submittedFileName);
InputStream testfileInputStream = part.getInputStream();
// make sure the temp directory exists
final Path tempPath = Paths.get("wdir", "temp");
Files.createDirectories(tempPath);

// conpy the file to the temp directory
final InputStream testfileInputStream = part.getInputStream();
final Path tempFile = tempPath.resolve(submittedFileName);
final String fileExtension =
FilenameUtils.getExtension(submittedFileName);
Files.copy(testfileInputStream, tempFile,
StandardCopyOption.REPLACE_EXISTING);

final Path outFilePath;

// java files have to be in the directory matching their
// qualified name
if ("[jJ][aA][vV][aA]".matches(fileExtension)) {
final String qualifiedName =
getQualifiedNameFromFile(tempFile);
String subDir =
StringUtils.replaceChars(qualifiedName, '.', '/');
subDir = subDir + "." + fileExtension;
outFilePath = outputDirectory.resolve(subDir);
} else {
// not needed in other languages
outFilePath = outputDirectory.resolve(submittedFileName);
}

Files.createDirectories(outFilePath.getParent());
Files.copy(testfileInputStream, outFilePath,

// move the file to its final position
Files.move(tempFile, outFilePath,
StandardCopyOption.REPLACE_EXISTING);
} catch (InvalidPathException | IOException e) {
throw new InternalActionErrorException(
Expand All @@ -434,9 +467,43 @@ protected void optionalWriteSubmittedFile(Part part, Path outputDirectory)
if (part == null) {
return;
}
String submittedFileName = part.getSubmittedFileName();
final String submittedFileName = part.getSubmittedFileName();
if ((submittedFileName != null) && !("".equals(submittedFileName))) {
writeSubmittedFile(part, outputDirectory);
}
}

/**
* Iterates over a java source file, and identifies the fully qualified
* name of the class from the package declaration in the source.
*
* @param sourceFile
* a java source file
* @return the fully qualified name of the class
* @throws IOException
* if the source file can not be read from.
*/
private String getQualifiedNameFromFile(Path sourceFile)
throws IOException {
final String packageRegex = "package\\s[^,;]+;";
LineIterator it;
String result = "";
it = FileUtils.lineIterator(sourceFile.toFile(), "UTF-8");

// look for the line identifying the package
while (it.hasNext()) {
final String line = it.nextLine();
if (line.matches(packageRegex)) {
result = line;
// strip not needed elements (the word package)
result = result.substring(8, result.length() - 1);
it.close();
result = result + ".";
break;
}
}
it.close();
// append the classname
return result + FilenameUtils.getBaseName(sourceFile.toString());
}
}
136 changes: 0 additions & 136 deletions wdir/course-0/exercise-2/tests/TestIFraction.java

This file was deleted.

Loading

0 comments on commit 1332733

Please sign in to comment.