Search in sources :

Example 6 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class DefaultInstallersProvider method setManualProxy.

/**
 * Sets the proxy manually
 *
 * @param installer the installer that requires configuration
 */
private void setManualProxy(final HttpSwordInstaller installer) {
    // set the host
    if (isNotBlank(this.proxyHost)) {
        installer.setProxyHost(this.proxyHost);
    }
    // set the port
    if (isNotBlank(this.proxyPort)) {
        try {
            final Integer p = Integer.parseInt(this.proxyPort);
            installer.setProxyPort(p.intValue());
        } catch (final NumberFormatException e) {
            throw new StepInternalException("Unable to parse port number " + this.proxyPort, e);
        }
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException)

Example 7 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class ImageController method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse response) throws ServletException, IOException {
    try {
        final String pathToImage = req.getRequestURI().substring(req.getContextPath().length() + req.getServletPath().length());
        final File image = new File(this.localSource, pathToImage);
        if (!image.exists()) {
            // fetch from web and write out
            download(image, pathToImage, response);
            return;
        }
        LOGGER.trace("Returning image locally stored at: ", image.getAbsolutePath());
        writeImage(image, response);
    } catch (final StepInternalException ex) {
        LOGGER.warn("An exception has occurred - image cannot be sent back", ex);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) File(java.io.File)

Example 8 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class FrontController method getControllerMethod.

/**
 * Returns the method to be invoked upon the controller
 *
 * @param methodName         the method name
 * @param controllerInstance the instance of the controller
 * @param args               the list of arguments, required to resolve the correct method if they have arguments
 * @param cacheKey           the key to retrieve in the cache
 * @return the method to be invoked
 */
Method getControllerMethod(final String methodName, final Object controllerInstance, final Object[] args, final String cacheKey) {
    final Class<?> controllerClass = controllerInstance.getClass();
    // retrieve method from cache, or put in cache if not there
    Method controllerMethod = this.methodNames.get(cacheKey);
    if (controllerMethod == null) {
        try {
            final Class<?>[] classes = getClasses(args);
            controllerMethod = controllerClass.getMethod(methodName, classes);
            // put method in cache
            this.methodNames.put(cacheKey, controllerMethod);
        } catch (final NoSuchMethodException e) {
            throw new StepInternalException("Unable to find matching method for " + methodName, e);
        }
    }
    return controllerMethod;
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) Method(java.lang.reflect.Method)

Example 9 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class FrontControllerTest method testDoGetHasException.

/**
 * tests what happens when doGet catches an exception
 */
@Test
public void testDoGetHasException() {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    final StepInternalException testException = new StepInternalException("A test exception");
    final FrontController fc = spy(this.fcUnderTest);
    final StepRequest parsedRequest = new StepRequest("blah", "SomeController", "someMethod", new String[] { "arg1", "arg2" });
    // TODO remove this/
    doNothing().when(fc).handleError(response, testException, mock(HttpServletRequest.class));
    // do the test
    fc.doGet(request, response);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 10 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class RequestUtils method validateSession.

/**
 * validates a session
 *
 * @param sessionProvider provides the client session
 */
public static void validateSession(final Provider<ClientSession> sessionProvider) {
    LOGGER.warn("Attempting to validate session from external call");
    try {
        final ClientSession clientSession = sessionProvider.get();
        final String requiredPassword = System.getProperty("step.setup.password");
        if (StringUtils.isNotBlank(requiredPassword)) {
            // check request has this parameter
            if (!requiredPassword.equals(clientSession.getParam("step.setup.password"))) {
                LOGGER.warn("DENYING ACCESS");
                throw new StepInternalException("This functionality is not available");
            }
        }
        final String ipAddress = clientSession.getIpAddress();
        final InetAddress addr = InetAddress.getByName(ipAddress);
        // Check if the address is a valid special local or loop back
        if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) {
            return;
        }
        // Check if the address is defined on any interface
        try {
            if (NetworkInterface.getByInetAddress(addr) != null) {
                return;
            }
        } catch (final SocketException e) {
            LOGGER.warn("Socket error: ", e);
        }
        LOGGER.warn("DENYING ACCESS TO IP ADDRESS [{}]", ipAddress);
        throw new StepInternalException("This functionality is not available");
    } catch (final UnknownHostException e) {
        throw new StepInternalException("Failed to initialise ip addresses", e);
    }
}
Also used : SocketException(java.net.SocketException) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) UnknownHostException(java.net.UnknownHostException) ClientSession(com.tyndalehouse.step.core.models.ClientSession) InetAddress(java.net.InetAddress)

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