Search in sources :

Example 71 with TypeToken

use of com.google.gson.reflect.TypeToken in project pwm by pwm-project.

the class NamedSecretValue method factory.

public static StoredValue.StoredValueFactory factory() {
    return new StoredValue.StoredValueFactory() {

        public NamedSecretValue fromJson(final String value) {
            try {
                final Map<String, NamedSecretData> values = JsonUtil.deserialize(value, new TypeToken<Map<String, NamedSecretData>>() {
                }.getType());
                final Map<String, NamedSecretData> linkedValues = new LinkedHashMap<>(values);
                return new NamedSecretValue(linkedValues);
            } catch (Exception e) {
                throw new IllegalStateException("NamedPasswordValue can not be json de-serialized: " + e.getMessage());
            }
        }

        public NamedSecretValue fromXmlElement(final Element settingElement, final PwmSecurityKey key) throws PwmOperationalException, PwmUnrecoverableException {
            final Map<String, NamedSecretData> values = new LinkedHashMap<>();
            final List<Element> valueElements = settingElement.getChildren("value");
            try {
                if (valueElements != null) {
                    for (final Element value : valueElements) {
                        if (value.getChild(ELEMENT_NAME) != null && value.getChild(ELEMENT_PASSWORD) != null) {
                            final String name = value.getChild(ELEMENT_NAME).getText();
                            final String encodedValue = value.getChild(ELEMENT_PASSWORD).getText();
                            final PasswordData passwordData = new PasswordData(SecureEngine.decryptStringValue(encodedValue, key, PwmBlockAlgorithm.CONFIG));
                            final List<Element> usages = value.getChildren(ELEMENT_USAGE);
                            final List<String> strUsages = new ArrayList<>();
                            if (usages != null) {
                                for (final Element usageElement : usages) {
                                    strUsages.add(usageElement.getText());
                                }
                            }
                            values.put(name, new NamedSecretData(passwordData, Collections.unmodifiableList(strUsages)));
                        }
                    }
                }
            } catch (Exception e) {
                final String errorMsg = "unable to decode encrypted password value for setting: " + e.getMessage();
                final ErrorInformation errorInfo = new ErrorInformation(PwmError.CONFIG_FORMAT_ERROR, errorMsg);
                throw new PwmOperationalException(errorInfo);
            }
            return new NamedSecretValue(values);
        }
    };
}
Also used : Element(org.jdom2.Element) ArrayList(java.util.ArrayList) PwmOperationalException(password.pwm.error.PwmOperationalException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) LinkedHashMap(java.util.LinkedHashMap) PwmOperationalException(password.pwm.error.PwmOperationalException) PwmSecurityKey(password.pwm.util.secure.PwmSecurityKey) ErrorInformation(password.pwm.error.ErrorInformation) NamedSecretData(password.pwm.config.value.data.NamedSecretData) PasswordData(password.pwm.util.PasswordData) TypeToken(com.google.gson.reflect.TypeToken)

Example 72 with TypeToken

use of com.google.gson.reflect.TypeToken in project teammates by TEAMMATES.

the class FeedbackResponseAdjustmentWorkerAction method execute.

@Override
public void execute() {
    String courseId = getRequestParamValue(ParamsNames.COURSE_ID);
    Assumption.assertPostParamNotNull(ParamsNames.COURSE_ID, courseId);
    String sessionName = getRequestParamValue(ParamsNames.FEEDBACK_SESSION_NAME);
    Assumption.assertPostParamNotNull(ParamsNames.FEEDBACK_SESSION_NAME, sessionName);
    String enrollmentDetails = getRequestParamValue(ParamsNames.ENROLLMENT_DETAILS);
    Assumption.assertPostParamNotNull(ParamsNames.ENROLLMENT_DETAILS, enrollmentDetails);
    log.info("Adjusting submissions for feedback session :" + sessionName + "in course : " + courseId);
    FeedbackSessionAttributes feedbackSession = logic.getFeedbackSession(sessionName, courseId);
    String errorString = "Error encountered while adjusting feedback session responses of %s in course %s: %s%n%s";
    if (feedbackSession == null) {
        log.severe(String.format(errorString, sessionName, courseId, "feedback session is null", ""));
        setForRetry();
        return;
    }
    List<FeedbackResponseAttributes> allResponses = logic.getFeedbackResponsesForSession(feedbackSession.getFeedbackSessionName(), feedbackSession.getCourseId());
    List<StudentEnrollDetails> enrollmentList = JsonUtils.fromJson(enrollmentDetails, new TypeToken<List<StudentEnrollDetails>>() {
    }.getType());
    for (FeedbackResponseAttributes response : allResponses) {
        try {
            logic.adjustFeedbackResponseForEnrollments(enrollmentList, response);
        } catch (Exception e) {
            String url = HttpRequestHelper.getRequestedUrl(request);
            Map<String, String[]> params = HttpRequestHelper.getParameterMap(request);
            // no logged-in user for worker
            String logMessage = new LogMessageGenerator().generateActionFailureLogMessage(url, params, e, null);
            log.severe(String.format(errorString, sessionName, courseId, e.getMessage(), logMessage));
            setForRetry();
            return;
        }
    }
}
Also used : FeedbackSessionAttributes(teammates.common.datatransfer.attributes.FeedbackSessionAttributes) FeedbackResponseAttributes(teammates.common.datatransfer.attributes.FeedbackResponseAttributes) TypeToken(com.google.gson.reflect.TypeToken) LogMessageGenerator(teammates.common.util.LogMessageGenerator) StudentEnrollDetails(teammates.common.datatransfer.StudentEnrollDetails) Map(java.util.Map)

Example 73 with TypeToken

use of com.google.gson.reflect.TypeToken in project route4me-java-sdk by route4me.

the class UsersManager method getUsers.

public List<User> getUsers(int limit, int offset) throws APIException {
    URIBuilder builder = Manager.defaultBuilder(USERS_EP);
    builder.setParameter("limit", Integer.toString(limit));
    builder.setParameter("offset", Integer.toString(offset));
    return this.makeRequest(RequestMethod.GET, builder, (HttpEntity) null, new TypeToken<ArrayList<User>>() {
    }.getType());
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 74 with TypeToken

use of com.google.gson.reflect.TypeToken in project LanternServer by LanternPowered.

the class ObjectTypeAdapterFactory method create.

@Nullable
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    if (!type.isAssignableFrom(this.typeToken)) {
        return null;
    }
    final TypeAdapter<JsonElement> jsonElementTypeAdapter = gson.getAdapter(JsonElement.class);
    final TypeToken theTypeToken = type;
    return new TypeAdapter<T>() {

        @SuppressWarnings("unchecked")
        @Override
        public void write(JsonWriter out, T value) throws IOException {
            final JsonElement element = serialize((TypeToken<V>) theTypeToken, (V) value, gson);
            jsonElementTypeAdapter.write(out, element);
        }

        @SuppressWarnings("unchecked")
        @Override
        public T read(JsonReader in) throws IOException {
            final JsonElement element = jsonElementTypeAdapter.read(in);
            return (T) deserialize(theTypeToken, element, gson);
        }
    };
}
Also used : JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) TypeAdapter(com.google.gson.TypeAdapter) JsonReader(com.google.gson.stream.JsonReader) JsonWriter(com.google.gson.stream.JsonWriter) Nullable(javax.annotation.Nullable)

Example 75 with TypeToken

use of com.google.gson.reflect.TypeToken in project yandex-money-sdk-java by yandex-money.

the class ShowcaseTypeAdapter method deserialize.

@Override
public Showcase deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject object = json.getAsJsonObject();
    Group form = null;
    JsonArray array = object.getAsJsonArray(MEMBER_FORM);
    if (array != null) {
        form = ListDelegate.deserialize(array, context);
    }
    List<AllowedMoneySource> moneySources = context.deserialize(object.get(MEMBER_MONEY_SOURCE), new TypeToken<List<AllowedMoneySource>>() {
    }.getType());
    List<Error> errors = ErrorTypeAdapter.getInstance().fromJson(object.getAsJsonArray(MEMBER_ERROR));
    return new Showcase.Builder().setTitle(getString(object, MEMBER_TITLE)).setHiddenFields(getNotNullMap(object, MEMBER_HIDDEN_FIELDS)).setForm(form).setMoneySources(toEmptyListIfNull(moneySources)).setErrors(toEmptyListIfNull(errors)).create();
}
Also used : JsonArray(com.google.gson.JsonArray) Group(com.yandex.money.api.model.showcase.components.containers.Group) AllowedMoneySource(com.yandex.money.api.model.AllowedMoneySource) TypeToken(com.google.gson.reflect.TypeToken) JsonObject(com.google.gson.JsonObject) JsonUtils.toJsonObject(com.yandex.money.api.typeadapters.JsonUtils.toJsonObject) Error(com.yandex.money.api.model.showcase.Showcase.Error)

Aggregations

TypeToken (com.google.gson.reflect.TypeToken)418 Gson (com.google.gson.Gson)178 Test (org.junit.Test)99 IOException (java.io.IOException)83 Map (java.util.Map)71 List (java.util.List)56 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)54 ArrayList (java.util.ArrayList)53 HashMap (java.util.HashMap)52 GsonBuilder (com.google.gson.GsonBuilder)45 File (java.io.File)34 Notebook (org.apache.zeppelin.notebook.Notebook)32 Type (java.lang.reflect.Type)31 FileNotFoundException (java.io.FileNotFoundException)29 Paragraph (org.apache.zeppelin.notebook.Paragraph)27 RestResponse (com.google.gerrit.acceptance.RestResponse)24 JsonElement (com.google.gson.JsonElement)24 JsonObject (com.google.gson.JsonObject)24 OutputStreamWriter (java.io.OutputStreamWriter)22 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)21