List of usage examples for org.apache.commons.httpclient.methods OptionsMethod getResponseHeader
@Override
public Header getResponseHeader(String headerName)
From source file:org.apache.wink.itest.headers.HeadersTest.java
public void testAllowHeaders() throws Exception { try {//from w w w. j a v a2s .c o m OptionsMethod httpMethod = new OptionsMethod(); httpMethod.setURI(new URI(getBaseURI() + "/headersallow1/allow1", false)); httpClient = new HttpClient(); try { int result = httpClient.executeMethod(httpMethod); assertEquals(204, result); assertNotNull(httpMethod.getResponseHeader("Allow")); List<String> allowedMethods = Arrays .asList(httpMethod.getResponseHeader("Allow").getValue().split(", ")); System.out.println(allowedMethods); assertEquals(3, allowedMethods.size()); assertTrue(allowedMethods.contains("HEAD")); assertTrue(allowedMethods.contains("OPTIONS")); assertTrue(allowedMethods.contains("GET")); } catch (IOException ioe) { ioe.printStackTrace(); fail(ioe.getMessage()); } finally { httpMethod.releaseConnection(); } } catch (URIException e) { e.printStackTrace(); fail(e.getMessage()); } try { OptionsMethod httpMethod = new OptionsMethod(); httpMethod.setURI(new URI(getBaseURI() + "/headersallow2", false)); httpClient = new HttpClient(); try { int result = httpClient.executeMethod(httpMethod); assertEquals(204, result); assertNotNull(httpMethod.getResponseHeader("Allow")); List<String> allowedMethods = Arrays .asList(httpMethod.getResponseHeader("Allow").getValue().split(", ")); System.out.println(allowedMethods); assertEquals(6, allowedMethods.size()); assertTrue(allowedMethods.contains("HEAD")); assertTrue(allowedMethods.contains("OPTIONS")); assertTrue(allowedMethods.contains("GET")); assertTrue(allowedMethods.contains("PUT")); assertTrue(allowedMethods.contains("POST")); assertTrue(allowedMethods.contains("DELETE")); } catch (IOException ioe) { ioe.printStackTrace(); fail(ioe.getMessage()); } finally { httpMethod.releaseConnection(); } } catch (URIException e) { e.printStackTrace(); fail(e.getMessage()); } try { OptionsMethod httpMethod = new OptionsMethod(); httpMethod.setURI(new URI(getBaseURI() + "/headersallow3/sublocator", false)); httpClient = new HttpClient(); try { int result = httpClient.executeMethod(httpMethod); assertEquals(204, result); assertNotNull(httpMethod.getResponseHeader("Allow")); List<String> allowedMethods = Arrays .asList(httpMethod.getResponseHeader("Allow").getValue().split(", ")); System.out.println(allowedMethods); assertEquals(3, allowedMethods.size()); assertTrue(allowedMethods.contains("HEAD")); assertTrue(allowedMethods.contains("OPTIONS")); assertTrue(allowedMethods.contains("GET")); assertEquals(null, httpMethod.getResponseBodyAsString()); } catch (IOException ioe) { ioe.printStackTrace(); fail(ioe.getMessage()); } finally { httpMethod.releaseConnection(); } } catch (URIException e) { e.printStackTrace(); fail(e.getMessage()); } }
From source file:org.apache.wink.itest.methodannotations.HttpMethodTest.java
/** * Tests that a OPTIONS request can be sent to resource annotated with a * custom OPTIONS annotation.// ww w. j a v a2 s . com */ public void testCustomOPTIONSRequest() { try { OptionsMethod httpMethod = new OptionsMethod(); httpMethod.setURI(new URI(ALT_URI, false)); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(httpMethod); System.out.println("Response status code: " + result); System.out.println("Response body: "); String responseBody = httpMethod.getResponseBodyAsString(); System.out.println(responseBody); assertEquals(200, result); assertEquals("", responseBody); Header header = httpMethod.getResponseHeader("Allow"); assertNotNull(header); String value = header.getValue(); assertTrue(value.contains("HEAD")); assertTrue(value.contains("OPTIONS")); assertTrue(value.contains("GET")); } catch (IOException ioe) { ioe.printStackTrace(); fail(ioe.getMessage()); } finally { httpMethod.releaseConnection(); } } catch (URIException e) { e.printStackTrace(); fail(e.getMessage()); } }
From source file:org.exoplatform.calendar.service.impl.RemoteCalendarServiceImpl.java
@Override public boolean isValidRemoteUrl(String url, String type, String remoteUser, String remotePassword) throws IOException, UnsupportedOperationException { try {/*w ww.j ava2s . co m*/ HttpClient client = new HttpClient(); HostConfiguration hostConfig = new HostConfiguration(); String host = new URL(url).getHost(); if (StringUtils.isEmpty(host)) host = url; hostConfig.setHost(host); client.setHostConfiguration(hostConfig); Credentials credentials = null; client.setHostConfiguration(hostConfig); if (!StringUtils.isEmpty(remoteUser)) { credentials = new UsernamePasswordCredentials(remoteUser, remotePassword); client.getState().setCredentials(new AuthScope(host, AuthScope.ANY_PORT, AuthScope.ANY_REALM), credentials); } if (CalendarService.ICALENDAR.equals(type)) { GetMethod get = new GetMethod(url); client.executeMethod(get); int statusCode = get.getStatusCode(); get.releaseConnection(); return (statusCode == HttpURLConnection.HTTP_OK); } else { if (CalendarService.CALDAV.equals(type)) { OptionsMethod options = new OptionsMethod(url); client.executeMethod(options); Header header = options.getResponseHeader("DAV"); options.releaseConnection(); if (header == null) { if (logger.isDebugEnabled()) { logger.debug("Cannot connect to remoter server or not support WebDav access"); } return false; } Boolean support = header.toString().contains("calendar-access"); options.releaseConnection(); if (!support) { if (logger.isDebugEnabled()) { logger.debug("Remote server does not support CalDav access"); } throw new UnsupportedOperationException("Remote server does not support CalDav access"); } return support; } return false; } } catch (MalformedURLException e) { if (logger.isDebugEnabled()) logger.debug(e.getMessage(), e); throw new IOException("URL is invalid. Maybe no legal protocol or URl could not be parsed"); } catch (IOException e) { if (logger.isDebugEnabled()) logger.debug(e.getMessage(), e); throw new IOException("Error occurs when connecting to remote server"); } }