use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.
the class ServletFormAuthTestCase method testServletFormAuth.
@Test
public void testServletFormAuth() throws IOException {
TestHttpClient client = new TestHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
return true;
}
return super.isRedirected(request, response, context);
}
});
try {
final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/test";
HttpGet get = new HttpGet(uri);
HttpResponse result = client.execute(get);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertTrue(response.startsWith("j_security_check"));
BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
final List<NameValuePair> data = new ArrayList<>();
data.addAll(Arrays.asList(pairs));
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/j_security_check;jsessionid=dsjahfklsahdfjklsa");
post.setEntity(new UrlEncodedFormEntity(data));
result = client.execute(post);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("user1", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.
the class ServletFormAuthTestCase method testServletFormAuthWithSavedPostBody.
@Test
public void testServletFormAuthWithSavedPostBody() throws IOException {
TestHttpClient client = new TestHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
return true;
}
return super.isRedirected(request, response, context);
}
});
try {
final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/echo";
HttpPost post = new HttpPost(uri);
post.setEntity(new StringEntity("String Entity"));
HttpResponse result = client.execute(post);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertTrue(response.startsWith("j_security_check"));
BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
final List<NameValuePair> data = new ArrayList<>();
data.addAll(Arrays.asList(pairs));
post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/j_security_check");
post.setEntity(new UrlEncodedFormEntity(data));
result = client.execute(post);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("String Entity", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.
the class ServletFormAuthURLRewriteTestCase method testServletFormAuth.
@Test
public void testServletFormAuth() throws IOException {
TestHttpClient client = new TestHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
return true;
}
return super.isRedirected(request, response, context);
}
});
try {
final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/test";
HttpGet get = new HttpGet(uri);
HttpResponse result = client.execute(get);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertTrue(response.startsWith("j_security_check"));
BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
final List<NameValuePair> data = new ArrayList<>();
data.addAll(Arrays.asList(pairs));
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/" + response);
post.setEntity(new UrlEncodedFormEntity(data));
result = client.execute(post);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
Assert.assertEquals("user1", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project undertow by undertow-io.
the class ServletFormAuthURLRewriteTestCase method testServletFormAuthWithOriginalRequestParams.
@Test
public void testServletFormAuthWithOriginalRequestParams() throws IOException {
TestHttpClient client = new TestHttpClient();
client.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
if (response.getStatusLine().getStatusCode() == StatusCodes.FOUND) {
return true;
}
return super.isRedirected(request, response, context);
}
});
try {
final String uri = DefaultServer.getDefaultServerURL() + "/servletContext/secured/echoParam?param=developer";
HttpPost post = new HttpPost(uri);
post.setEntity(new StringEntity("String Entity"));
HttpResponse result = client.execute(post);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
String response = HttpClientUtils.readResponse(result);
Assert.assertTrue(response.startsWith("j_security_check"));
BasicNameValuePair[] pairs = new BasicNameValuePair[] { new BasicNameValuePair("j_username", "user1"), new BasicNameValuePair("j_password", "password1") };
final List<NameValuePair> data = new ArrayList<>();
data.addAll(Arrays.asList(pairs));
post = new HttpPost(DefaultServer.getDefaultServerURL() + "/servletContext/" + response);
post.setEntity(new UrlEncodedFormEntity(data));
result = client.execute(post);
assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
response = HttpClientUtils.readResponse(result);
assertEquals("developer", response);
} finally {
client.getConnectionManager().shutdown();
}
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project jackrabbit by apache.
the class RepositoryServiceImpl method copy.
@Override
public void copy(SessionInfo sessionInfo, String srcWorkspaceName, NodeId srcNodeId, NodeId destParentNodeId, Name destName) throws RepositoryException {
if (srcWorkspaceName.equals(sessionInfo.getWorkspaceName())) {
super.copy(sessionInfo, srcWorkspaceName, srcNodeId, destParentNodeId, destName);
return;
}
HttpPost request = null;
try {
request = new HttpPost(getWorkspaceURI(sessionInfo));
request.setHeader("Referer", request.getURI().toASCIIString());
addIfHeader(sessionInfo, request);
NamePathResolver resolver = getNamePathResolver(sessionInfo);
StringBuilder args = new StringBuilder();
args.append(srcWorkspaceName);
args.append(",");
args.append(resolver.getJCRPath(getPath(srcNodeId, sessionInfo, srcWorkspaceName)));
args.append(",");
String destParentPath = resolver.getJCRPath(getPath(destParentNodeId, sessionInfo));
String destPath = (destParentPath.endsWith("/") ? destParentPath + resolver.getJCRName(destName) : destParentPath + "/" + resolver.getJCRName(destName));
args.append(destPath);
List<BasicNameValuePair> nvps = Collections.singletonList(new BasicNameValuePair(PARAM_COPY, args.toString()));
HttpEntity entity = new UrlEncodedFormEntity(nvps, Charset.forName("UTF-8"));
request.setEntity(entity);
HttpResponse response = executeRequest(sessionInfo, request);
request.checkSuccess(response);
} catch (IOException e) {
throw new RepositoryException(e);
} catch (DavException e) {
throw ExceptionConverter.generate(e, request);
} finally {
if (request != null) {
request.releaseConnection();
}
}
}
Aggregations