use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class PartialDate method getPartialDateFromArray.
/**
* Depending on the number of parts, it creates a Partial date with year/month/day resolution
*
* @param parts the array of parts each representing part of the date
* @param negativeDate true if the date is BC
* @return the newly created PartialDate
*/
private static PartialDate getPartialDateFromArray(final String[] parts, final boolean negativeDate) {
final LocalDateTime translatedTime;
PrecisionType p;
final int multiplier = negativeDate ? -1 : 1;
try {
// length of field determines how much of the date has been specified
switch(parts.length) {
case NO_PARTS:
throw new StepInternalException("There weren't enough parts to this date");
case YEAR:
// only the year is specified, so use 1st of Jan Year
translatedTime = new LocalDateTime(multiplier * parseInt(parts[0]), 1, 1, 0, 0);
p = PrecisionType.YEAR;
break;
case YEAR_AND_MONTH:
translatedTime = new LocalDateTime(multiplier * parseInt(parts[0]), parseInt(parts[1]), 1, 0, 0);
p = PrecisionType.MONTH;
break;
case YEAR_MONTH_AND_DAY:
translatedTime = new LocalDateTime(multiplier * parseInt(parts[0]), parseInt(parts[1]), parseInt(parts[2]), 0, 0);
p = PrecisionType.DAY;
break;
default:
throw new StepInternalException("Too many parts to the date: ");
}
} catch (final NumberFormatException nfe) {
throw new StepInternalException("Could not parse date into year, month or day.", nfe);
}
return new PartialDate(translatedTime, p);
}
use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class ImageController method writeImage.
/**
* writes the image to the response stream.
*
* @param image the image
* @param response the response stream
*/
private void writeImage(final File image, final HttpServletResponse response) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(image);
writeResponse(image, response, fileInputStream);
} catch (final IOException e) {
response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
throw new StepInternalException("Failed to write image to output response stream", e);
} finally {
IOUtils.closeQuietly(fileInputStream);
}
}
use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class ImageController method download.
/**
* Downloads the image locally from the server.
*
* @param image the image file
* @param pathToImage the path to the image
* @param response the response for the user
*/
private void download(final File image, final String pathToImage, final HttpServletResponse response) {
final HttpGet get = new HttpGet(this.remoteSource + pathToImage);
final DefaultHttpClient client = new DefaultHttpClient();
// two streams for downloading
OutputStream fileOutput = null;
InputStream inputStream = null;
try {
final HttpResponse remoteResponse = client.execute(get);
if (remoteResponse.getStatusLine().getStatusCode() != 200) {
response.setStatus(remoteResponse.getStatusLine().getStatusCode());
throw new StepInternalException("Unable to obtain image remotely");
}
final HttpEntity entity = remoteResponse.getEntity();
inputStream = entity.getContent();
long contentLength = entity.getContentLength();
if (contentLength < 0) {
contentLength = Integer.MAX_VALUE;
}
// read the input from the external source
final int imageDataLength = (int) contentLength;
final byte[] imageData = new byte[imageDataLength];
inputStream.read(imageData, 0, imageDataLength);
// write to the file
fileOutput = new FileOutputStream(image);
fileOutput.write(imageData, 0, imageDataLength);
// while we have the data, let's write it to the response
prepareImageResponse(imageDataLength, image.getName(), response);
response.getOutputStream().write(imageData, 0, imageDataLength);
} catch (final IOException e) {
throw new StepInternalException("Unable to obtain image remotely", e);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(fileOutput);
}
}
use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class InternationalJsonController method readBundle.
/**
* Read bundle.
*
* @param locale the locale
* @return the string
*/
private String readBundle(final Locale locale, final String... bundleNames) {
List<ResourceBundle> bundles = new ArrayList<ResourceBundle>(bundleNames.length);
for (String b : bundleNames) {
bundles.add(ResourceBundle.getBundle(b, locale));
}
final JsonResourceBundle jsonResourceBundle = new JsonResourceBundle(bundles);
String jsonResponse;
try {
jsonResponse = objectMapper.writeValueAsString(jsonResourceBundle);
} catch (final IOException e) {
throw new StepInternalException("Unable to read messages", e);
}
return "var __s = " + jsonResponse;
}
use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class SiteMapController method getSiteMap.
/**
* Gets the site map.
*
* @param mapType the map type
* @param specifier the specifier that says which initial should be generated
* @return the site map
*/
// public byte[] getSiteMap(final SiteMapType mapType, final char specifier) {
public byte[] getSiteMap() {
final StringBuilder siteMap = new StringBuilder(10 * 1024 * 1024);
initSiteMap(siteMap);
addVersions(siteMap);
// addVersions(siteMap, BookCategory.COMMENTARY, specifier);
// switch (mapType) {
// case SITEMAP_COMMENTARY:
// addVersions(siteMap, BookCategory.COMMENTARY, specifier);
// break;
// case SITEMAP_BIBLE:
// addUrl(siteMap, null, null, null, "/versions.jsp");
// addVersions(siteMap, BookCategory.BIBLE, specifier);
// break;
// default:
// break;
// }
closeSiteMap(siteMap);
try {
return siteMap.toString().getBytes("UTF-8");
} catch (final UnsupportedEncodingException e) {
throw new StepInternalException("Unable to convert to UTF-8", e);
}
}
Aggregations