Permalink
Browse files

Modifying app upload to have broader compatibility for v1 cloud contr…

…ollers [CF-129]

- uses the POST / _method=put  mechanism by default

- falls back to PUT if POST fails to support legacy Micro Cloud Foundry 1.2 and earlier

Change-Id: I9c08ab61283d2dfa05fff58f382df3332032f1e0
  • Loading branch information...
1 parent 7e242d4 commit 7f695c3f881773399ed86be948b8fbc78c7812a9 @trisberg trisberg committed Oct 24, 2012
@@ -248,11 +248,13 @@ public void handleError(ClientHttpResponse response) throws IOException {
ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
if (response.getBody() != null) {
try {
- @SuppressWarnings("unchecked")
- Map<String, Object> map = mapper.readValue(response.getBody(), Map.class);
- exception.setDescription(CloudUtil.parse(String.class, map.get("description")));
+ @SuppressWarnings("unchecked")
+ Map<String, Object> map = mapper.readValue(response.getBody(), Map.class);
+ exception.setDescription(CloudUtil.parse(String.class, map.get("description")));
} catch (JsonParseException e) {
exception.setDescription("Client error");
+ } catch (IOException e) {
+ exception.setDescription("Client error");
}
} else {
exception.setDescription("Client error");
@@ -40,6 +40,7 @@
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
@@ -303,17 +304,24 @@ public void uploadApplication(String appName, ApplicationArchive archive, Upload
callback.onMatchedFileNames(knownRemoteResources.getFilenames());
UploadApplicationPayload payload = new UploadApplicationPayload(archive, knownRemoteResources);
callback.onProcessMatchedResources(payload.getTotalUncompressedSize());
- HttpEntity<?> entity = generatePartialResourceRequest(payload, knownRemoteResources);
+ HttpEntity<?> entity = generatePartialResourceRequest(payload, knownRemoteResources, HttpMethod.POST);
String url = getUrl("apps/{appName}/application");
try {
- getRestTemplate().put(url, entity, appName);
+ getRestTemplate().postForLocation(url, entity, appName);
} catch (HttpServerErrorException hsee) {
if (HttpStatus.INTERNAL_SERVER_ERROR.equals(hsee.getStatusCode())) {
// this is for supporting legacy Micro Cloud Foundry 1.1 and older
- uploadAppUsingLegacyApi(url, entity, appName);
+ uploadAppUsingLegacyApi(url, payload, knownRemoteResources, appName);
} else {
throw hsee;
}
+ } catch (HttpClientErrorException hcee) {
+ if (HttpStatus.NOT_FOUND.equals(hcee.getStatusCode())) {
+ // this is for supporting legacy Micro Cloud Foundry 1.2
+ uploadAppUsingLegacyApi(url, payload, knownRemoteResources, appName);
+ } else {
+ throw hcee;
+ }
}
}
@@ -496,16 +504,21 @@ private void doUploadApplicationZipFile(String appName, File file, UploadStatusC
* the content type as JSON works fine.
*
* @param path app path
- * @param entity HttpEntity for the payload
+ * @param payload the application payload
+ * @param knownRemoteResources known remote resources
* @param appName name of app
* @throws HttpServerErrorException
*/
- private void uploadAppUsingLegacyApi(String path, HttpEntity<?> entity, String appName) throws HttpServerErrorException {
+ private void uploadAppUsingLegacyApi(String path,
+ UploadApplicationPayload payload,
+ CloudResources knownRemoteResources,
+ String appName) throws HttpServerErrorException, IOException {
RestTemplate legacyRestTemplate = new RestTemplate();
legacyRestTemplate.setRequestFactory(this.getRestTemplate().getRequestFactory());
legacyRestTemplate.setErrorHandler(new ErrorHandler());
legacyRestTemplate.setMessageConverters(getLegacyMessageConverters());
- legacyRestTemplate.put(path, entity, appName);
+ HttpEntity<?> entity = generatePartialResourceRequest(payload, knownRemoteResources, HttpMethod.PUT);
+ legacyRestTemplate.put(path, entity, appName);
}
/**
@@ -538,8 +551,11 @@ private CloudResources getKnownRemoteResources(ApplicationArchive archive) throw
}
private HttpEntity<MultiValueMap<String, ?>> generatePartialResourceRequest(UploadApplicationPayload application,
- CloudResources knownRemoteResources) throws IOException {
+ CloudResources knownRemoteResources, HttpMethod method) throws IOException {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>(2);
+ if (method == HttpMethod.POST) {
+ body.add("_method", "put");
+ }
if (application.getNumEntries() > 0) {
//If the entire app contents are cached, send nothing
body.add("application", application);
@@ -548,6 +564,7 @@ private CloudResources getKnownRemoteResources(ApplicationArchive archive) throw
String knownRemoteResourcesPayload = mapper.writeValueAsString(knownRemoteResources);
body.add("resources", knownRemoteResourcesPayload);
HttpHeaders headers = new HttpHeaders();
+ headers.add("Accept", "text/html, application/xml");
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
return new HttpEntity<MultiValueMap<String, ?>>(body, headers);
}
@@ -238,12 +238,20 @@ public void logout() {
public void uploadApplication() throws IOException {
String appName = namespacedAppName(TEST_NAMESPACE, "travel_test3");
CloudApplication app = createAndUploadTestApp(appName);
+ String serviceName = "test_database";
+ createDatabaseService(serviceName);
+ client.updateApplicationServices(appName, Collections.singletonList(serviceName));
assertNotNull(app);
assertEquals(AppState.STOPPED, app.getState());
String url = computeAppUrlNoProtocol(ccUrl, appName);
assertEquals(url, app.getUris().get(0));
+
+ client.startApplication(appName);
+ app = client.getApplication(appName);
+ assertEquals(AppState.STARTED, app.getState());
+
}
@Test

0 comments on commit 7f695c3

Please sign in to comment.