Search in sources :

Example 26 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project keycloak by keycloak.

the class RealmLocalizationResource method createOrUpdateRealmLocalizationTextsFromFile.

/**
 * Import localization from uploaded JSON file
 */
@POST
@Path("{locale}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void createOrUpdateRealmLocalizationTextsFromFile(@PathParam("locale") String locale, MultipartFormDataInput input) {
    this.auth.realm().requireManageRealm();
    Map<String, List<InputPart>> formDataMap = input.getFormDataMap();
    if (!formDataMap.containsKey("file")) {
        throw new BadRequestException();
    }
    InputPart file = formDataMap.get("file").get(0);
    try (InputStream inputStream = file.getBody(InputStream.class, null)) {
        TypeReference<HashMap<String, String>> typeRef = new TypeReference<HashMap<String, String>>() {
        };
        Map<String, String> rep = JsonSerialization.readValue(inputStream, typeRef);
        realm.createOrUpdateRealmLocalizationTexts(locale, rep);
    } catch (IOException e) {
        throw new BadRequestException("Could not read file.");
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) HashMap(java.util.HashMap) InputStream(java.io.InputStream) BadRequestException(javax.ws.rs.BadRequestException) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

InputPart (org.jboss.resteasy.plugins.providers.multipart.InputPart)26 Test (org.junit.Test)13 InputStream (java.io.InputStream)12 List (java.util.List)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 File (java.io.File)6 MultipartFormDataInput (org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput)6 BadRequestException (uk.gov.justice.services.adapter.rest.exception.BadRequestException)6 Arrays.asList (java.util.Arrays.asList)5 Collections.emptyList (java.util.Collections.emptyList)5 Collections.singletonList (java.util.Collections.singletonList)5 GenericType (org.jboss.resteasy.util.GenericType)4 BufferedInputStream (java.io.BufferedInputStream)3 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 EventSink (org.candlepin.audit.EventSink)3 ManifestManager (org.candlepin.controller.ManifestManager)3 ConflictOverrides (org.candlepin.sync.ConflictOverrides)3