Example usage for org.springframework.web.client ResourceAccessException ResourceAccessException

List of usage examples for org.springframework.web.client ResourceAccessException ResourceAccessException

Introduction

In this page you can find the example usage for org.springframework.web.client ResourceAccessException ResourceAccessException.

Prototype

public ResourceAccessException(String msg, IOException ex) 

Source Link

Document

Construct a new ResourceAccessException with the given message and IOException .

Usage

From source file:de.codecentric.boot.admin.web.servlet.resource.ResourcePatternResolvingResourceResolver.java

@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
        List<? extends Resource> locations, ResourceResolverChain chain) {
    try {/*from   ww  w .j  ava  2s . co  m*/
        Resource[] resources = resourcePatternResolver.getResources(pattern);
        return chain.resolveResource(request, requestPath, Arrays.asList(resources));
    } catch (IOException ex) {
        throw new ResourceAccessException("Couldn't resolve resources for \"" + pattern + "\"", ex);
    }
}

From source file:de.codecentric.boot.admin.web.servlet.resource.ConcatenatingResourceResolver.java

@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath,
        List<? extends Resource> locations, ResourceResolverChain chain) {
    String filename = StringUtils.getFilename(requestPath);
    try {// www  .jav a  2s .  c om
        return new InMemoryFileResource(filename, buildDescription(locations), getContent(locations),
                getLastModified(locations));
    } catch (IOException ex) {
        throw new ResourceAccessException("Couldn't concatenate resources [" + locations + "]", ex);
    }
}

From source file:com.kajj.tools.logviewer.LogViewerRestController.java

/**
 * Returns the last <code>numberOfLines</code> from the specified log file. The log file must
 * be in the containers log directory to be read.
 *
 * @param fileName the name of the log file.
 * @param numberOfLines the number of lines to return in the tail.
 * @return The last <code>numberOfLines</code> of the specified log file.
 *///from w  w  w . ja  v a 2 s  . co m
@RequestMapping(value = "/logs/{fileName}/tail", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
public ResponseEntity<String> tailLog(@PathVariable final String fileName,
        @RequestParam(value = "lines", defaultValue = "100") final int numberOfLines) {
    try {
        final List<String> logs = logRepository.getTailLog(fileName, numberOfLines);

        final StringBuilder tail = new StringBuilder();
        int i = 1;
        for (final String log : logs) {
            tail.append(i++).append(": ").append(log).append("\n");
        }

        return new ResponseEntity(tail.toString(), HttpStatus.OK);
    } catch (final IOException ioe) {
        throw new ResourceAccessException("Unable to read log file", ioe);
    }
}

From source file:com.alexshabanov.springrestapi.restapitest.RestOperationsTestClient.java

@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
        ResponseExtractor<T> responseExtractor) throws RestClientException {
    try {/*from w w w. ja  v  a2s.  c o  m*/
        final ClientHttpRequest clientHttpRequest = createRequest(url, method);

        if (requestCallback != null) {
            requestCallback.doWithRequest(clientHttpRequest);
        }

        final MockHttpServletResponse mockHttpServletResponse = testSupport
                .handle(toMockHttpServletRequest(url, method, clientHttpRequest));

        final ClientHttpResponse clientHttpResponse = new ClientHttpResponseAdapter(mockHttpServletResponse);

        // translate error statuses
        if (getErrorHandler().hasError(clientHttpResponse)) {
            getErrorHandler().handleError(clientHttpResponse);
        }

        return responseExtractor == null ? null : responseExtractor.extractData(clientHttpResponse);
    } catch (IOException e) {
        throw new ResourceAccessException("Can't access the resource provided: " + url, e);
    }
}

From source file:org.kaaproject.kaa.server.common.admin.KaaRestTemplate.java

@Override
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
        ResponseExtractor<T> responseExtractor) throws ResourceAccessException {
    int maxRetry = hosts.length;
    while (true) {
        try {/*from   w  w  w .  j  a v a  2s .  com*/
            return super.doExecute(url, method, requestCallback, responseExtractor);
        } catch (ResourceAccessException ex) {
            logger.info("Connect to ({}:{}) failed", getCurHost(), getCurPort(), ex);
            boolean isRequestFactorySet = false;
            while (!isRequestFactorySet) {
                if (index < hosts.length - 1) {
                    index++;
                } else {
                    index = 0;
                }
                logger.info("Trying connect to ({}:{})", getCurHost(), getCurPort(), ex);
                if (maxRetry <= 0) {
                    logger.error("Failed to connect to ({}:{})", getCurHost(), getCurPort(), ex);
                    throw new ResourceAccessException(
                            "I/O error on " + method.name() + " request for \"" + url + "\":" + ex.getMessage(),
                            new IOException(ex));
                } else {
                    maxRetry--;
                }
                try {
                    setNewRequestFactory(index);
                } catch (Exception exception) {
                    logger.info("Failed to initialize new request factory ({}:{})", getCurHost(), getCurPort(),
                            exception);
                    continue;
                }
                url = updateUrl(url);
                isRequestFactorySet = true;
            }
        } catch (RestClientException ex) {
            throw ex;
        }
    }
}

From source file:org.eclipse.cft.server.core.internal.client.CloudFoundryClientFactory.java

private static String getJson(RestTemplate restTemplate, String urlString) {
    ClientHttpResponse response = null;/*from   w w  w  . j av a2s . c  om*/
    HttpMethod method = null;
    try {
        method = HttpMethod.GET;

        URI url = new UriTemplate(urlString).expand();
        ClientHttpRequest request = restTemplate.getRequestFactory().createRequest(url, method);

        List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
        acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
        request.getHeaders().setAccept(acceptableMediaTypes);
        //if (requestCallback != null) {
        //   requestCallback.doWithRequest(request);
        //}
        response = request.execute();
        if (response.getBody() != null) {
            HttpMessageConverterExtractor<String> extractor = new HttpMessageConverterExtractor<String>(
                    String.class, restTemplate.getMessageConverters());
            String data = extractor.extractData(response);
            return data;
        }
        ;
    } catch (IOException ex) {
        throw new ResourceAccessException(
                "I/O error on " + method.name() + " request for \"" + urlString + "\":" + ex.getMessage(), ex);
    } finally {
        if (response != null) {
            response.close();
        }
    }
    return null;
}

From source file:org.springframework.web.client.RestTemplate.java

/**
 * Execute the given method on the provided URI.
 * <p>The {@link ClientHttpRequest} is processed using the {@link RequestCallback};
 * the response with the {@link ResponseExtractor}.
 * @param url the fully-expanded URL to connect to
 * @param method the HTTP method to execute (GET, POST, etc.)
 * @param requestCallback object that prepares the request (can be {@code null})
 * @param responseExtractor object that extracts the return value from the response (can be {@code null})
 * @return an arbitrary object, as returned by the {@link ResponseExtractor}
 *//*from  w w  w  . j av a  2s . c o m*/
protected <T> T doExecute(URI url, HttpMethod method, RequestCallback requestCallback,
        ResponseExtractor<T> responseExtractor) throws RestClientException {

    Assert.notNull(url, "'url' must not be null");
    Assert.notNull(method, "'method' must not be null");
    ClientHttpResponse response = null;
    try {
        ClientHttpRequest request = createRequest(url, method);
        if (requestCallback != null) {
            requestCallback.doWithRequest(request);
        }
        response = request.execute();
        if (!getErrorHandler().hasError(response)) {
            logResponseStatus(method, url, response);
        } else {
            handleResponseError(method, url, response);
        }
        if (responseExtractor != null) {
            return responseExtractor.extractData(response);
        } else {
            return null;
        }
    } catch (IOException ex) {
        throw new ResourceAccessException(
                "I/O error on " + method.name() + " request for \"" + url + "\": " + ex.getMessage(), ex);
    } finally {
        if (response != null) {
            response.close();
        }
    }
}

From source file:com.jaspersoft.android.sdk.client.JsRestClient.java

private void downloadFile(URI uri, File file) throws RestClientException {
    ClientHttpResponse response = null;//from   ww w.j  a  v  a 2s  .co  m
    try {
        ClientHttpRequest request = restTemplate.getRequestFactory().createRequest(uri, HttpMethod.GET);
        response = request.execute();
        if (restTemplate.getErrorHandler().hasError(response)) {
            restTemplate.getErrorHandler().handleError(response);
        }
        copyResponseToFile(response, file);
    } catch (IOException ex) {
        throw new ResourceAccessException("I/O error: " + ex.getMessage(), ex);
    } finally {
        if (response != null)
            response.close();
    }
}