Skip to content

Commit

Permalink
added new method for upload multipart files
Browse files Browse the repository at this point in the history
  • Loading branch information
sudhakaropsmx committed Jan 21, 2021
1 parent c13b1bf commit 925cbb4
Showing 1 changed file with 47 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,29 @@

package com.netflix.spinnaker.gate.controllers

import org.apache.commons.io.IOUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.http.HttpHeaders
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile

import com.netflix.spinnaker.gate.config.ServiceConfiguration
import com.netflix.spinnaker.gate.services.internal.OpsmxOesService
import com.netflix.spinnaker.security.AuthenticatedRequest

import groovy.util.logging.Slf4j
import io.swagger.annotations.ApiOperation
import okio.Buffer
import okio.BufferedSink
import okhttp3.Headers
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request

import okio.Buffer
import okio.BufferedSink
import okio.Okio
import okio.Source

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import retrofit.client.Response

@RequestMapping("/oes")
@RestController
Expand Down Expand Up @@ -142,7 +145,7 @@ class OpsmxOesController {
@RequestMapping(value = "/accountsConfig/addOrUpdateDynamicAccount", method = RequestMethod.POST)
String addOrUpdateAccount(@RequestParam MultipartFile files, @RequestParam Map<String, String> postData) {
String filename = files ? files.getOriginalFilename() : ''
return addOrUpdateDynamicAccount(files, postData.get("postData"), )
return addOrUpdateDynamicAccount(files, postData.get("postData"))
}

@ApiOperation(value = "Endpoint for Oes rest services")
Expand Down Expand Up @@ -231,8 +234,36 @@ class OpsmxOesController {
return opsmxOesService.updateOesResponse6(type, source, source1, source2, source3, data)
}

@ApiOperation(value = "Add or Update dynamic account configured in Spinnaker", response = String.class )
@RequestMapping(value = "/accountsConfig/cloudProviders/addOrUpdateDynamicAccount", method = RequestMethod.POST)
String addOrUpdateCloudProver(@RequestParam MultipartFile files, @RequestParam Map<String, String> postData) {
String filename = files ? files.getOriginalFilename() : ''
return addOrUpdateCloudProverAccount(files, postData.get("postData"))
}

private String addOrUpdateCloudProverAccount(MultipartFile files, String data) {
Map<String, Optional<String>> authenticationHeaders = AuthenticatedRequest.getAuthenticationHeaders();
Map headersMap = new HashMap()
authenticationHeaders.each { key, val ->
if(val.isPresent())
headersMap.putAt(key,val.get())
else
headersMap.putAt(key,"")
}
AuthenticatedRequest.propagate {
def request = new Request.Builder()
.url(serviceConfiguration.getServiceEndpoint("opsmx").url +"/oes/accountsConfig/cloudProviders/addOrUpdateDynamicAccount")
.headers(Headers.of(headersMap))
.post(uploadFileOkHttp(data,files))
.build()

def response = okHttpClient.newCall(request).execute()
return response.body()?.string() ?: "Unknown reason: " + response.code()
}.call() as String
}

private String addOrUpdateDynamicAccount(MultipartFile files, String data) {

Map<String, Optional<String>> authenticationHeaders = AuthenticatedRequest.getAuthenticationHeaders();
Map headersMap = new HashMap()
authenticationHeaders.each { key, val ->
Expand All @@ -247,14 +278,14 @@ class OpsmxOesController {
.headers(Headers.of(headersMap))
.post(uploadFileOkHttp(data,files))
.build()

def response = okHttpClient.newCall(request).execute()
return response.body()?.string() ?: "Unknown reason: " + response.code()
}.call() as String
}.call() as String
}

private okhttp3.RequestBody uploadFileOkHttp(String data, MultipartFile multiPartfile) throws IOException {

String fileName = multiPartfile.getOriginalFilename();
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
Expand All @@ -263,7 +294,7 @@ class OpsmxOesController {
public MediaType contentType() {
return MediaType.parse("application/octet-stream");
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
try {
Expand Down Expand Up @@ -291,4 +322,5 @@ class OpsmxOesController {
builder.addFormDataPart("postData", null, okhttp3.RequestBody.create(MediaType.parse("text/plain"), data));
return builder.build();
}

}

0 comments on commit 925cbb4

Please sign in to comment.