Java Code Examples for org.apache.commons.httpclient.methods.PutMethod#addRequestHeader()
The following examples show how to use
org.apache.commons.httpclient.methods.PutMethod#addRequestHeader() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3SignedUrlFileUploader.java From teamcity-s3-artifact-storage-plugin with Apache License 2.0 | 6 votes |
private void uploadArtifact(@NotNull final String artifactPath, @NotNull final URL uploadUrl, @NotNull final File file, @NotNull final HttpClient awsHttpClient) throws IOException { try { final PutMethod putMethod = new PutMethod(uploadUrl.toString()); putMethod.addRequestHeader("User-Agent", "TeamCity Agent"); putMethod.setRequestEntity(new FileRequestEntity(file, S3Util.getContentType(file))); HttpClientCloseUtil.executeAndReleaseConnection(awsHttpClient, putMethod); LOG.debug(String.format("Successfully uploaded artifact %s to %s", artifactPath, uploadUrl)); } catch (HttpClientCloseUtil.HttpErrorCodeException e) { final String msg; if (e.getResponseCode() == HttpStatus.SC_FORBIDDEN) { msg = "Failed to upload artifact " + artifactPath + ": received response code HTTP 403. Ensure that the credentials in S3 storage profile are correct."; } else { msg = "Failed to upload artifact " + artifactPath + " to " + uploadUrl + ": received response code HTTP " + e.getResponseCode() + "."; } LOG.info(msg); throw new IOException(msg); } }
Example 2
Source File: CoursesFoldersITCase.java From olat with Apache License 2.0 | 6 votes |
@Test public void testUploadFile() throws IOException, URISyntaxException { final HttpClient c = loginWithCookie("administrator", "olat"); final URI uri = UriBuilder.fromUri(getNodeURI()).path("files").build(); // create single page final URL fileUrl = RepositoryEntriesITCase.class.getResource("singlepage.html"); assertNotNull(fileUrl); final File file = new File(fileUrl.toURI()); final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA); final Part[] parts = { new FilePart("file", file), new StringPart("filename", file.getName()) }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final int code = c.executeMethod(method); assertEquals(code, 200); final OlatNamedContainerImpl folder = BCCourseNode.getNodeFolderContainer((BCCourseNode) bcNode, course1.getCourseEnvironment()); final VFSItem item = folder.resolve(file.getName()); assertNotNull(item); }
Example 3
Source File: RepositoryEntriesITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testImportBlog() throws HttpException, IOException, URISyntaxException { final URL cpUrl = RepositoryEntriesITCase.class.getResource("blog-demo.zip"); assertNotNull(cpUrl); final File cp = new File(cpUrl.toURI()); final HttpClient c = loginWithCookie("administrator", "olat"); final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA); final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "blog-demo.zip"), new StringPart("resourcename", "Blog demo"), new StringPart("displayname", "Blog demo") }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class); assertNotNull(vo); final Long key = vo.getKey(); final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key); assertNotNull(re); assertNotNull(re.getOwnerGroup()); assertNotNull(re.getOlatResource()); assertEquals("Blog demo", re.getDisplayname()); log.info(re.getOlatResource().getResourceableTypeName()); }
Example 4
Source File: RepositoryEntriesITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testImportQuestionnaire() throws HttpException, IOException, URISyntaxException { final URL cpUrl = RepositoryEntriesITCase.class.getResource("questionnaire-demo.zip"); assertNotNull(cpUrl); final File cp = new File(cpUrl.toURI()); final HttpClient c = loginWithCookie("administrator", "olat"); final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA); final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "questionnaire-demo.zip"), new StringPart("resourcename", "Questionnaire demo"), new StringPart("displayname", "Questionnaire demo") }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class); assertNotNull(vo); final Long key = vo.getKey(); final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key); assertNotNull(re); assertNotNull(re.getOwnerGroup()); assertNotNull(re.getOlatResource()); assertEquals("Questionnaire demo", re.getDisplayname()); log.info(re.getOlatResource().getResourceableTypeName()); }
Example 5
Source File: UserMgmtITCase.java From olat with Apache License 2.0 | 5 votes |
/** * Test machine format for gender and date */ @Test public void testCreateUser2() throws IOException { final HttpClient c = loginWithCookie("administrator", "olat"); final UserVO vo = new UserVO(); final String username = UUID.randomUUID().toString(); vo.setLogin(username); vo.setFirstName("John"); vo.setLastName("Smith"); vo.setEmail(username + "@frentix.com"); vo.putProperty("telOffice", "39847592"); vo.putProperty("telPrivate", "39847592"); vo.putProperty("telMobile", "39847592"); vo.putProperty("gender", "female");// male or female vo.putProperty("birthDay", "20091212"); final String stringuifiedAuth = stringuified(vo); final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true); final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(entity); method.addRequestHeader("Accept-Language", "en"); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final UserVO savedVo = parse(body, UserVO.class); final Identity savedIdent = securityManager.findIdentityByName(username); assertNotNull(savedVo); assertNotNull(savedIdent); assertEquals(savedVo.getKey(), savedIdent.getKey()); assertEquals(savedVo.getLogin(), savedIdent.getName()); assertEquals("Female", userService.getUserProperty(savedIdent.getUser(), UserConstants.GENDER, Locale.ENGLISH)); assertEquals("39847592", userService.getUserProperty(savedIdent.getUser(), UserConstants.TELPRIVATE, Locale.ENGLISH)); assertEquals("12/12/09", userService.getUserProperty(savedIdent.getUser(), UserConstants.BIRTHDAY, Locale.ENGLISH)); }
Example 6
Source File: RepositoryEntriesITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testImportCp() throws HttpException, IOException, URISyntaxException { final URL cpUrl = RepositoryEntriesITCase.class.getResource("cp-demo.zip"); assertNotNull(cpUrl); final File cp = new File(cpUrl.toURI()); final HttpClient c = loginWithCookie("administrator", "olat"); final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA); final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "cp-demo.zip"), new StringPart("resourcename", "CP demo"), new StringPart("displayname", "CP demo") }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class); assertNotNull(vo); final Long key = vo.getKey(); final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key); assertNotNull(re); assertNotNull(re.getOwnerGroup()); assertNotNull(re.getOlatResource()); assertEquals("CP demo", re.getDisplayname()); }
Example 7
Source File: CatalogITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testPutCatalogEntryJson() throws IOException { final RepositoryEntry re = createRepository("put-cat-entry-json", 6458438l); final HttpClient c = loginWithCookie("administrator", "olat"); final CatalogEntryVO subEntry = new CatalogEntryVO(); subEntry.setName("Sub-entry-1"); subEntry.setDescription("Sub-entry-description-1"); subEntry.setType(CatalogEntry.TYPE_NODE); subEntry.setRepositoryEntryKey(re.getKey()); final String entity = stringuified(subEntry); final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build(); final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.APPLICATION_JSON); final RequestEntity requestEntity = new StringRequestEntity(entity, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(requestEntity); final int code = c.executeMethod(method); assertEquals(200, code); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final CatalogEntryVO vo = parse(body, CatalogEntryVO.class); assertNotNull(vo); final List<CatalogEntry> children = catalogService.getChildrenOf(entry1); CatalogEntry ce = null; for (final CatalogEntry child : children) { if (vo.getKey().equals(child.getKey())) { ce = child; break; } } assertNotNull(ce); assertNotNull(ce.getRepositoryEntry()); assertEquals(re.getKey(), ce.getRepositoryEntry().getKey()); }
Example 8
Source File: CatalogITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testPutCatalogEntryJson() throws IOException { final RepositoryEntry re = createRepository("put-cat-entry-json", 6458438l); final HttpClient c = loginWithCookie("administrator", "olat"); final CatalogEntryVO subEntry = new CatalogEntryVO(); subEntry.setName("Sub-entry-1"); subEntry.setDescription("Sub-entry-description-1"); subEntry.setType(CatalogEntry.TYPE_NODE); subEntry.setRepositoryEntryKey(re.getKey()); final String entity = stringuified(subEntry); final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build(); final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.APPLICATION_JSON); final RequestEntity requestEntity = new StringRequestEntity(entity, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(requestEntity); final int code = c.executeMethod(method); assertEquals(200, code); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final CatalogEntryVO vo = parse(body, CatalogEntryVO.class); assertNotNull(vo); final List<CatalogEntry> children = catalogService.getChildrenOf(entry1); CatalogEntry ce = null; for (final CatalogEntry child : children) { if (vo.getKey().equals(child.getKey())) { ce = child; break; } } assertNotNull(ce); assertNotNull(ce.getRepositoryEntry()); assertEquals(re.getKey(), ce.getRepositoryEntry().getKey()); }
Example 9
Source File: CatalogITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testPutCategoryJson() throws IOException { final HttpClient c = loginWithCookie("administrator", "olat"); final CatalogEntryVO subEntry = new CatalogEntryVO(); subEntry.setName("Sub-entry-1"); subEntry.setDescription("Sub-entry-description-1"); subEntry.setType(CatalogEntry.TYPE_NODE); final String entity = stringuified(subEntry); final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build(); final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.APPLICATION_JSON); final RequestEntity requestEntity = new StringRequestEntity(entity, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(requestEntity); final int code = c.executeMethod(method); assertEquals(200, code); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final CatalogEntryVO vo = parse(body, CatalogEntryVO.class); assertNotNull(vo); final List<CatalogEntry> children = catalogService.getChildrenOf(entry1); boolean saved = false; for (final CatalogEntry child : children) { if (vo.getKey().equals(child.getKey())) { saved = true; break; } } assertTrue(saved); }
Example 10
Source File: RepositoryEntriesITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testImportWiki() throws HttpException, IOException, URISyntaxException { final URL cpUrl = RepositoryEntriesITCase.class.getResource("wiki-demo.zip"); assertNotNull(cpUrl); final File cp = new File(cpUrl.toURI()); final HttpClient c = loginWithCookie("administrator", "olat"); final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA); final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "wiki-demo.zip"), new StringPart("resourcename", "Wiki demo"), new StringPart("displayname", "Wiki demo") }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class); assertNotNull(vo); final Long key = vo.getKey(); final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key); assertNotNull(re); assertNotNull(re.getOwnerGroup()); assertNotNull(re.getOlatResource()); assertEquals("Wiki demo", re.getDisplayname()); log.info(re.getOlatResource().getResourceableTypeName()); }
Example 11
Source File: UserMgmtITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testCreateUser() throws IOException { final HttpClient c = loginWithCookie("administrator", "olat"); final UserVO vo = new UserVO(); final String username = UUID.randomUUID().toString(); vo.setLogin(username); vo.setFirstName("John"); vo.setLastName("Smith"); vo.setEmail(username + "@frentix.com"); vo.putProperty("telOffice", "39847592"); vo.putProperty("telPrivate", "39847592"); vo.putProperty("telMobile", "39847592"); vo.putProperty("gender", "Female");// male or female vo.putProperty("birthDay", "12/12/2009"); final String stringuifiedAuth = stringuified(vo); final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true); final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(entity); method.addRequestHeader("Accept-Language", "en"); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final UserVO savedVo = parse(body, UserVO.class); final Identity savedIdent = securityManager.findIdentityByName(username); assertNotNull(savedVo); assertNotNull(savedIdent); assertEquals(savedVo.getKey(), savedIdent.getKey()); assertEquals(savedVo.getLogin(), savedIdent.getName()); assertEquals("Female", userService.getUserProperty(savedIdent.getUser(), UserConstants.GENDER, Locale.ENGLISH)); assertEquals("39847592", userService.getUserProperty(savedIdent.getUser(), UserConstants.TELPRIVATE, Locale.ENGLISH)); assertEquals("12/12/09", userService.getUserProperty(savedIdent.getUser(), UserConstants.BIRTHDAY, Locale.ENGLISH)); }
Example 12
Source File: AbstractSubmarineServerTest.java From submarine with Apache License 2.0 | 5 votes |
protected static PutMethod httpPut(String path, String body, String user, String pwd) throws IOException { LOG.info("Connecting to {}", URL + path); HttpClient httpClient = new HttpClient(); PutMethod putMethod = new PutMethod(URL + path); putMethod.addRequestHeader("Origin", URL); putMethod.setRequestHeader("Content-type", "application/yaml"); RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8")); putMethod.setRequestEntity(entity); if (userAndPasswordAreNotBlank(user, pwd)) { putMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd)); } httpClient.executeMethod(putMethod); LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText()); return putMethod; }
Example 13
Source File: RepositoryEntriesITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testImportWiki() throws HttpException, IOException, URISyntaxException { final URL cpUrl = RepositoryEntriesITCase.class.getResource("wiki-demo.zip"); assertNotNull(cpUrl); final File cp = new File(cpUrl.toURI()); final HttpClient c = loginWithCookie("administrator", "olat"); final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA); final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "wiki-demo.zip"), new StringPart("resourcename", "Wiki demo"), new StringPart("displayname", "Wiki demo") }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class); assertNotNull(vo); final Long key = vo.getKey(); final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key); assertNotNull(re); assertNotNull(re.getOwnerGroup()); assertNotNull(re.getOlatResource()); assertEquals("Wiki demo", re.getDisplayname()); log.info(re.getOlatResource().getResourceableTypeName()); }
Example 14
Source File: UserMgmtITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testCreateUser() throws IOException { final HttpClient c = loginWithCookie("administrator", "olat"); final UserVO vo = new UserVO(); final String username = UUID.randomUUID().toString(); vo.setLogin(username); vo.setFirstName("John"); vo.setLastName("Smith"); vo.setEmail(username + "@frentix.com"); vo.putProperty("telOffice", "39847592"); vo.putProperty("telPrivate", "39847592"); vo.putProperty("telMobile", "39847592"); vo.putProperty("gender", "Female");// male or female vo.putProperty("birthDay", "12/12/2009"); final String stringuifiedAuth = stringuified(vo); final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true); final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(entity); method.addRequestHeader("Accept-Language", "en"); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final UserVO savedVo = parse(body, UserVO.class); final Identity savedIdent = securityManager.findIdentityByName(username); assertNotNull(savedVo); assertNotNull(savedIdent); assertEquals(savedVo.getKey(), savedIdent.getKey()); assertEquals(savedVo.getLogin(), savedIdent.getName()); assertEquals("Female", userService.getUserProperty(savedIdent.getUser(), UserConstants.GENDER, Locale.ENGLISH)); assertEquals("39847592", userService.getUserProperty(savedIdent.getUser(), UserConstants.TELPRIVATE, Locale.ENGLISH)); assertEquals("12/12/09", userService.getUserProperty(savedIdent.getUser(), UserConstants.BIRTHDAY, Locale.ENGLISH)); }
Example 15
Source File: RepositoryEntriesITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testImportTest() throws HttpException, IOException, URISyntaxException { final URL cpUrl = RepositoryEntriesITCase.class.getResource("qti-demo.zip"); assertNotNull(cpUrl); final File cp = new File(cpUrl.toURI()); final HttpClient c = loginWithCookie("administrator", "olat"); final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA); final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "qti-demo.zip"), new StringPart("resourcename", "QTI demo"), new StringPart("displayname", "QTI demo") }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class); assertNotNull(vo); final Long key = vo.getKey(); final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key); assertNotNull(re); assertNotNull(re.getOwnerGroup()); assertNotNull(re.getOlatResource()); assertEquals("QTI demo", re.getDisplayname()); log.info(re.getOlatResource().getResourceableTypeName()); }
Example 16
Source File: RepositoryEntriesITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testImportCp() throws HttpException, IOException, URISyntaxException { final URL cpUrl = RepositoryEntriesITCase.class.getResource("cp-demo.zip"); assertNotNull(cpUrl); final File cp = new File(cpUrl.toURI()); final HttpClient c = loginWithCookie("administrator", "olat"); final PutMethod method = createPut("repo/entries", MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.MULTIPART_FORM_DATA); final Part[] parts = { new FilePart("file", cp), new StringPart("filename", "cp-demo.zip"), new StringPart("resourcename", "CP demo"), new StringPart("displayname", "CP demo") }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final RepositoryEntryVO vo = parse(body, RepositoryEntryVO.class); assertNotNull(vo); final Long key = vo.getKey(); final RepositoryEntry re = RepositoryServiceImpl.getInstance().lookupRepositoryEntry(key); assertNotNull(re); assertNotNull(re.getOwnerGroup()); assertNotNull(re.getOlatResource()); assertEquals("CP demo", re.getDisplayname()); }
Example 17
Source File: OlatJerseyTestCase.java From olat with Apache License 2.0 | 5 votes |
public PutMethod createPut(final URI requestURI, final String accept, final boolean cookie) { final PutMethod method = new PutMethod(requestURI.toString()); if (cookie) { method.getParams().setCookiePolicy(CookiePolicy.RFC_2109); } if (StringHelper.containsNonWhitespace(accept)) { method.addRequestHeader("Accept", accept); } method.addRequestHeader("Accept-Language", "en"); return method; }
Example 18
Source File: ZeppelinServerMock.java From zeppelin with Apache License 2.0 | 5 votes |
protected static PutMethod httpPut(String path, String body, String user, String pwd) throws IOException { LOG.info("Connecting to {}", URL + path); HttpClient httpClient = new HttpClient(); PutMethod putMethod = new PutMethod(URL + path); putMethod.addRequestHeader("Origin", URL); RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8")); putMethod.setRequestEntity(entity); if (userAndPasswordAreNotBlank(user, pwd)) { putMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd)); } httpClient.executeMethod(putMethod); LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText()); return putMethod; }
Example 19
Source File: CatalogITCase.java From olat with Apache License 2.0 | 5 votes |
@Test public void testPutCategoryJson() throws IOException { final HttpClient c = loginWithCookie("administrator", "olat"); final CatalogEntryVO subEntry = new CatalogEntryVO(); subEntry.setName("Sub-entry-1"); subEntry.setDescription("Sub-entry-description-1"); subEntry.setType(CatalogEntry.TYPE_NODE); final String entity = stringuified(subEntry); final URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).build(); final PutMethod method = createPut(uri, MediaType.APPLICATION_JSON, true); method.addRequestHeader("Content-Type", MediaType.APPLICATION_JSON); final RequestEntity requestEntity = new StringRequestEntity(entity, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(requestEntity); final int code = c.executeMethod(method); assertEquals(200, code); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final CatalogEntryVO vo = parse(body, CatalogEntryVO.class); assertNotNull(vo); final List<CatalogEntry> children = catalogService.getChildrenOf(entry1); boolean saved = false; for (final CatalogEntry child : children) { if (vo.getKey().equals(child.getKey())) { saved = true; break; } } assertTrue(saved); }
Example 20
Source File: UploadBitsCommand.java From orion.server with Eclipse Public License 1.0 | 4 votes |
@Override protected ServerStatus _doIt() { /* multi server status */ MultiServerStatus status = new MultiServerStatus(); try { /* upload project contents */ File appPackage = packager.getDeploymentPackage(super.getApplication(), appStore, command); deployedAppPackageName = PackageUtils.getApplicationPackageType(appStore); if (appPackage == null) { status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to read application content", null)); return status; } URI targetURI = URIUtil.toURI(target.getUrl()); PutMethod uploadMethod = new PutMethod(targetURI.resolve("/v2/apps/" + getApplication().getGuid() + "/bits?async=true").toString()); uploadMethod.addRequestHeader(new Header("Authorization", "bearer " + target.getCloud().getAccessToken().getString("access_token"))); Part[] parts = {new StringPart(CFProtocolConstants.V2_KEY_RESOURCES, "[]"), new FilePart(CFProtocolConstants.V2_KEY_APPLICATION, appPackage)}; uploadMethod.setRequestEntity(new MultipartRequestEntity(parts, uploadMethod.getParams())); /* send request */ ServerStatus jobStatus = HttpUtil.executeMethod(uploadMethod); status.add(jobStatus); if (!jobStatus.isOK()) return status; /* long running task, keep track */ int attemptsLeft = MAX_ATTEMPTS; JSONObject resp = jobStatus.getJsonData(); String taksStatus = resp.getJSONObject(CFProtocolConstants.V2_KEY_ENTITY).getString(CFProtocolConstants.V2_KEY_STATUS); while (!CFProtocolConstants.V2_KEY_FINISHED.equals(taksStatus) && !CFProtocolConstants.V2_KEY_FAILURE.equals(taksStatus)) { if (CFProtocolConstants.V2_KEY_FAILED.equals(taksStatus)) { /* delete the tmp file */ appPackage.delete(); status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Upload failed", null)); return status; } if (attemptsLeft == 0) { /* delete the tmp file */ appPackage.delete(); status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Upload timeout exceeded", null)); return status; } /* two seconds */ Thread.sleep(2000); /* check whether job has finished */ URI jobLocation = targetURI.resolve(resp.getJSONObject(CFProtocolConstants.V2_KEY_METADATA).getString(CFProtocolConstants.V2_KEY_URL)); GetMethod jobRequest = new GetMethod(jobLocation.toString()); ServerStatus confStatus = HttpUtil.configureHttpMethod(jobRequest, target.getCloud()); if (!confStatus.isOK()) return confStatus; /* send request */ jobStatus = HttpUtil.executeMethod(jobRequest); status.add(jobStatus); if (!jobStatus.isOK()) return status; resp = jobStatus.getJsonData(); taksStatus = resp.getJSONObject(CFProtocolConstants.V2_KEY_ENTITY).getString(CFProtocolConstants.V2_KEY_STATUS); --attemptsLeft; } if (CFProtocolConstants.V2_KEY_FAILURE.equals(jobStatus)) { status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to upload application bits", null)); return status; } /* delete the tmp file */ appPackage.delete(); return status; } catch (Exception e) { String msg = NLS.bind("An error occured when performing operation {0}", commandName); //$NON-NLS-1$ logger.error(msg, e); status.add(new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e)); return status; } }