use of com.google.gson.JsonSyntaxException in project che by eclipse.
the class FactoryServiceTest method shouldThrowBadRequestExceptionWhenInvalidFactorySectionProvided.
@Test
public void shouldThrowBadRequestExceptionWhenInvalidFactorySectionProvided() throws Exception {
doThrow(new JsonSyntaxException("Invalid json")).when(factoryBuilderSpy).build(any(InputStream.class));
final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).multiPart("factory", "invalid content", FACTORY_IMAGE_MIME_TYPE).expect().statusCode(400).when().post(SERVICE_PATH);
final ServiceError err = getFromResponse(response, ServiceError.class);
assertEquals(err.getMessage(), "Invalid JSON value of the field 'factory' provided");
}
use of com.google.gson.JsonSyntaxException in project MinecraftForge by MinecraftForge.
the class ModListHelper method parseListFile.
private static void parseListFile(String listFile) {
File f;
try {
if (listFile.startsWith("absolute:"))
f = new File(listFile.substring(9)).getCanonicalFile();
else
f = new File(mcDirectory, listFile).getCanonicalFile();
} catch (IOException e2) {
FMLRelaunchLog.log(Level.INFO, e2, "Unable to canonicalize path %s relative to %s", listFile, mcDirectory.getAbsolutePath());
return;
}
if (!f.exists()) {
FMLRelaunchLog.info("Failed to find modList file %s", f.getAbsolutePath());
return;
}
if (visitedFiles.contains(f)) {
FMLRelaunchLog.severe("There appears to be a loop in the modListFile hierarchy. You shouldn't do this!");
throw new RuntimeException("Loop detected, impossible to load modlistfile");
}
String json;
try {
json = Files.asCharSource(f, Charsets.UTF_8).read();
} catch (IOException e1) {
FMLRelaunchLog.log(Level.INFO, e1, "Failed to read modList json file %s.", listFile);
return;
}
Gson gsonParser = new Gson();
JsonModList modList;
try {
modList = gsonParser.fromJson(json, JsonModList.class);
} catch (JsonSyntaxException e) {
FMLRelaunchLog.log(Level.INFO, e, "Failed to parse modList json file %s.", listFile);
return;
}
visitedFiles.add(f);
// We visit parents before children, so the additionalMods list is sorted from parent to child
if (modList.parentList != null) {
parseListFile(modList.parentList);
}
File repoRoot = new File(modList.repositoryRoot);
if (!repoRoot.exists()) {
FMLRelaunchLog.info("Failed to find the specified repository root %s", modList.repositoryRoot);
return;
}
for (String s : modList.modRef) {
StringBuilder fileName = new StringBuilder();
StringBuilder genericName = new StringBuilder();
String[] parts = s.split(":");
fileName.append(parts[0].replace('.', File.separatorChar));
genericName.append(parts[0]);
fileName.append(File.separatorChar);
fileName.append(parts[1]).append(File.separatorChar);
genericName.append(":").append(parts[1]);
fileName.append(parts[2]).append(File.separatorChar);
fileName.append(parts[1]).append('-').append(parts[2]);
if (parts.length == 4) {
fileName.append('-').append(parts[3]);
genericName.append(":").append(parts[3]);
}
fileName.append(".jar");
tryAddFile(fileName.toString(), repoRoot, genericName.toString());
}
}
use of com.google.gson.JsonSyntaxException in project SneakerBot by Penor.
the class Config method load.
public static ArrayList<ConfigObject> load(String name) {
File file = new File(name);
if (!file.exists()) {
System.out.println(name + " does not exist; One has been created for you.");
create(name);
return null;
}
Type type = new TypeToken<ArrayList<ConfigObject>>() {
}.getType();
try {
return new GsonBuilder().create().fromJson(new FileReader(name), type);
} catch (JsonIOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
use of com.google.gson.JsonSyntaxException in project SneakerBot by Penor.
the class Credentials method load.
public static HashMap<String, CredentialObject> load(String name) {
File file = new File(name);
if (!file.exists()) {
System.out.println(name + " does not exist; One has been created for you.");
create(name);
return null;
}
Type type = new TypeToken<HashMap<String, CredentialObject>>() {
}.getType();
try {
return new GsonBuilder().create().fromJson(new FileReader(name), type);
} catch (JsonIOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
use of com.google.gson.JsonSyntaxException in project pratilipi by Pratilipi.
the class GenericApi method executeApi.
final Object executeApi(GenericApi api, Method apiMethod, JsonObject requestPayloadJson, Class<? extends GenericRequest> apiMethodParameterType, HttpServletRequest request) {
try {
GenericRequest apiRequest = new Gson().fromJson(requestPayloadJson, apiMethodParameterType);
if (apiRequest instanceof GenericFileUploadRequest) {
GenericFileUploadRequest gfuRequest = (GenericFileUploadRequest) apiRequest;
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(request);
while (iterator.hasNext()) {
FileItemStream fileItemStream = iterator.next();
if (!fileItemStream.isFormField()) {
gfuRequest.setName(fileItemStream.getName());
gfuRequest.setData(IOUtils.toByteArray(fileItemStream.openStream()));
gfuRequest.setMimeType(fileItemStream.getContentType());
break;
}
}
} catch (IOException | FileUploadException e) {
throw new UnexpectedServerException();
}
}
JsonObject errorMessages = apiRequest.validate();
if (errorMessages.entrySet().size() > 0)
return new InvalidArgumentException(errorMessages);
else
return apiMethod.invoke(api, apiRequest);
} catch (JsonSyntaxException e) {
logger.log(Level.SEVERE, "Invalid JSON in request body.", e);
return new InvalidArgumentException("Invalid JSON in request body.");
} catch (UnexpectedServerException e) {
return e;
} catch (InvocationTargetException e) {
Throwable te = e.getTargetException();
if (te instanceof InvalidArgumentException || te instanceof InsufficientAccessException || te instanceof UnexpectedServerException) {
return te;
} else {
logger.log(Level.SEVERE, "Failed to execute API.", te);
return new UnexpectedServerException();
}
} catch (IllegalAccessException | IllegalArgumentException e) {
logger.log(Level.SEVERE, "Failed to execute API.", e);
return new UnexpectedServerException();
}
}
Aggregations