Search in sources :

Example 56 with StepInternalException

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);
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException)

Example 57 with StepInternalException

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);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 58 with StepInternalException

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);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) HttpEntity(org.apache.http.HttpEntity) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 59 with StepInternalException

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;
}
Also used : JsonResourceBundle(com.tyndalehouse.step.rest.framework.JsonResourceBundle) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) JsonResourceBundle(com.tyndalehouse.step.rest.framework.JsonResourceBundle) IOException(java.io.IOException)

Example 60 with StepInternalException

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);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)62 IOException (java.io.IOException)25 Book (org.crosswire.jsword.book.Book)9 Key (org.crosswire.jsword.passage.Key)7 EntityDoc (com.tyndalehouse.step.core.data.EntityDoc)5 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 ParseException (org.apache.lucene.queryParser.ParseException)4 BookException (org.crosswire.jsword.book.BookException)4 Versification (org.crosswire.jsword.versification.Versification)4 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)3 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)3 KeyWrapper (com.tyndalehouse.step.core.models.KeyWrapper)3 FileInputStream (java.io.FileInputStream)3 TransformingSAXEventProvider (org.crosswire.common.xml.TransformingSAXEventProvider)3 XMLUtil.writeToString (org.crosswire.common.xml.XMLUtil.writeToString)3 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)3 BibleBook (org.crosswire.jsword.versification.BibleBook)3 AllResultsCollector (com.tyndalehouse.step.core.data.AllResultsCollector)2