All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.whispersystems.signalservice.internal.websocket.DefaultResponseMapper Maven / Gradle / Ivy

There is a newer version: 2.15.3_unofficial_107
Show newest version
package org.whispersystems.signalservice.internal.websocket;


import org.whispersystems.signalservice.api.push.exceptions.MalformedResponseException;
import org.whispersystems.signalservice.internal.ServiceResponse;
import org.whispersystems.signalservice.internal.util.JsonUtil;

import java.util.Objects;
import java.util.function.Function;

/**
 * A default implementation of a {@link ResponseMapper} that can parse most known
 * application errors via {@link DefaultErrorMapper} and provides basic JSON parsing of the
 * response model if possible.
 * 

* Can be extended to add custom parsing for both the result type and the error cases. *

* See {@link #extend(Class)} and {@link DefaultErrorMapper#extend()}. */ public class DefaultResponseMapper implements ResponseMapper { private final Class clazz; private final ErrorMapper errorMapper; private final CustomResponseMapper customResponseMapper; public static DefaultResponseMapper getDefault(Class clazz) { return new DefaultResponseMapper<>(clazz); } public static DefaultResponseMapper.Builder extend(Class clazz) { return new DefaultResponseMapper.Builder<>(clazz); } private DefaultResponseMapper(Class clazz) { this(clazz, null, DefaultErrorMapper.getDefault()); } private DefaultResponseMapper(Class clazz, CustomResponseMapper customResponseMapper, ErrorMapper errorMapper) { this.clazz = clazz; this.customResponseMapper = customResponseMapper; this.errorMapper = errorMapper; } @Override public ServiceResponse map(int status, String body, Function getHeader, boolean unidentified) { Throwable applicationError; try { applicationError = errorMapper.parseError(status, body, getHeader); } catch (MalformedResponseException e) { applicationError = e; } if (applicationError == null) { try { if (customResponseMapper != null) { return Objects.requireNonNull(customResponseMapper.map(status, body, getHeader, unidentified)); } return ServiceResponse.forResult(JsonUtil.fromJsonResponse(body, clazz), status, body); } catch (MalformedResponseException e) { applicationError = e; } } return ServiceResponse.forApplicationError(applicationError, status, body); } public static class Builder { private final Class clazz; private DefaultErrorMapper.Builder errorMapperBuilder = DefaultErrorMapper.extend(); private CustomResponseMapper customResponseMapper; public Builder(Class clazz) { this.clazz = clazz; } public Builder withResponseMapper(CustomResponseMapper responseMapper) { this.customResponseMapper = responseMapper; return this; } public Builder withCustomError(int status, ErrorMapper errorMapper) { errorMapperBuilder.withCustom(status, errorMapper); return this; } public ResponseMapper build() { return new DefaultResponseMapper<>(clazz, customResponseMapper, errorMapperBuilder.build()); } } public interface CustomResponseMapper { ServiceResponse map(int status, String body, Function getHeader, boolean unidentified) throws MalformedResponseException; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy