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);
}
};
}
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;
}
}
}
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());
}
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);
}
};
}
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();
}
Aggregations