Search in sources :

Example 66 with TypeToken

use of com.google.common.reflect.TypeToken in project zeppelin by apache.

the class NotebookRestApi method putNotePermissions.

/**
   * set note authorization information
   */
@PUT
@Path("{noteId}/permissions")
@ZeppelinApi
public Response putNotePermissions(@PathParam("noteId") String noteId, String req) throws IOException {
    String principal = SecurityUtils.getPrincipal();
    HashSet<String> roles = SecurityUtils.getRoles();
    HashSet<String> userAndRoles = new HashSet<>();
    userAndRoles.add(principal);
    userAndRoles.addAll(roles);
    checkIfUserIsAnon(getBlockNotAuthenticatedUserErrorMsg());
    checkIfUserIsOwner(noteId, ownerPermissionError(userAndRoles, notebookAuthorization.getOwners(noteId)));
    HashMap<String, HashSet<String>> permMap = gson.fromJson(req, new TypeToken<HashMap<String, HashSet<String>>>() {
    }.getType());
    Note note = notebook.getNote(noteId);
    LOG.info("Set permissions {} {} {} {} {}", noteId, principal, permMap.get("owners"), permMap.get("readers"), permMap.get("writers"));
    HashSet<String> readers = permMap.get("readers");
    HashSet<String> owners = permMap.get("owners");
    HashSet<String> writers = permMap.get("writers");
    // Set readers, if writers and owners is empty -> set to user requesting the change
    if (readers != null && !readers.isEmpty()) {
        if (writers.isEmpty()) {
            writers = Sets.newHashSet(SecurityUtils.getPrincipal());
        }
        if (owners.isEmpty()) {
            owners = Sets.newHashSet(SecurityUtils.getPrincipal());
        }
    }
    // Set writers, if owners is empty -> set to user requesting the change
    if (writers != null && !writers.isEmpty()) {
        if (owners.isEmpty()) {
            owners = Sets.newHashSet(SecurityUtils.getPrincipal());
        }
    }
    notebookAuthorization.setReaders(noteId, readers);
    notebookAuthorization.setWriters(noteId, writers);
    notebookAuthorization.setOwners(noteId, owners);
    LOG.debug("After set permissions {} {} {}", notebookAuthorization.getOwners(noteId), notebookAuthorization.getReaders(noteId), notebookAuthorization.getWriters(noteId));
    AuthenticationInfo subject = new AuthenticationInfo(SecurityUtils.getPrincipal());
    note.persist(subject);
    notebookServer.broadcastNote(note);
    notebookServer.broadcastNoteList(subject, userAndRoles);
    return new JsonResponse<>(Status.OK).build();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) AuthenticationInfo(org.apache.zeppelin.user.AuthenticationInfo) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) PUT(javax.ws.rs.PUT)

Example 67 with TypeToken

use of com.google.common.reflect.TypeToken in project weave by continuuity.

the class ArgumentsCodec method deserialize.

@Override
public Arguments deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    List<String> arguments = context.deserialize(jsonObj.get("arguments"), new TypeToken<List<String>>() {
    }.getType());
    Map<String, Collection<String>> args = context.deserialize(jsonObj.get("runnableArguments"), new TypeToken<Map<String, Collection<String>>>() {
    }.getType());
    ImmutableMultimap.Builder<String, String> builder = ImmutableMultimap.builder();
    for (Map.Entry<String, Collection<String>> entry : args.entrySet()) {
        builder.putAll(entry.getKey(), entry.getValue());
    }
    return new Arguments(arguments, builder.build());
}
Also used : TypeToken(com.google.common.reflect.TypeToken) Arguments(com.continuuity.weave.internal.Arguments) JsonObject(com.google.gson.JsonObject) Collection(java.util.Collection) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Map(java.util.Map)

Example 68 with TypeToken

use of com.google.common.reflect.TypeToken in project weave by continuuity.

the class WeaveSpecificationCodec method deserialize.

@Override
public WeaveSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObj = json.getAsJsonObject();
    String name = jsonObj.get("name").getAsString();
    Map<String, RuntimeSpecification> runnables = context.deserialize(jsonObj.get("runnables"), new TypeToken<Map<String, RuntimeSpecification>>() {
    }.getType());
    List<WeaveSpecification.Order> orders = context.deserialize(jsonObj.get("orders"), new TypeToken<List<WeaveSpecification.Order>>() {
    }.getType());
    JsonElement handler = jsonObj.get("handler");
    EventHandlerSpecification eventHandler = null;
    if (handler != null && !handler.isJsonNull()) {
        eventHandler = context.deserialize(handler, EventHandlerSpecification.class);
    }
    return new DefaultWeaveSpecification(name, runnables, orders, eventHandler);
}
Also used : TypeToken(com.google.common.reflect.TypeToken) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) EventHandlerSpecification(com.continuuity.weave.api.EventHandlerSpecification) DefaultEventHandlerSpecification(com.continuuity.weave.internal.DefaultEventHandlerSpecification) RuntimeSpecification(com.continuuity.weave.api.RuntimeSpecification) DefaultWeaveSpecification(com.continuuity.weave.internal.DefaultWeaveSpecification)

Example 69 with TypeToken

use of com.google.common.reflect.TypeToken in project EventHub by Codecademy.

the class BatchTrackEvent method execute.

@Override
public synchronized void execute(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    List<Map<String, String>> events = gson.fromJson(request.getParameter("events"), new TypeToken<List<Map<String, String>>>() {
    }.getType());
    List<Long> eventIds = Lists.newArrayList();
    PrintWriter writer = response.getWriter();
    for (Map<String, String> eventMap : events) {
        String date = eventMap.get("date");
        if (date == null) {
            date = dateHelper.getDate();
        }
        Event event = new Event.Builder(eventMap.get("event_type"), eventMap.get("external_user_id"), date, eventMap).build();
        eventIds.add(eventHub.addEvent(event));
    }
    writer.println(gson.toJson(eventIds));
}
Also used : TypeToken(com.google.common.reflect.TypeToken) Event(com.codecademy.eventhub.model.Event) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 70 with TypeToken

use of com.google.common.reflect.TypeToken in project retrofit by square.

the class RxJava2CallAdapterFactoryTest method responseTypes.

@Test
public void responseTypes() {
    Type oBodyClass = new TypeToken<Observable<String>>() {
    }.getType();
    assertThat(factory.get(oBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sBodyClass = new TypeToken<Single<String>>() {
    }.getType();
    assertThat(factory.get(sBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mBodyClass = new TypeToken<Maybe<String>>() {
    }.getType();
    assertThat(factory.get(sBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fBodyClass = new TypeToken<Flowable<String>>() {
    }.getType();
    assertThat(factory.get(sBodyClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oBodyWildcard = new TypeToken<Observable<? extends String>>() {
    }.getType();
    assertThat(factory.get(oBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sBodyWildcard = new TypeToken<Single<? extends String>>() {
    }.getType();
    assertThat(factory.get(sBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mBodyWildcard = new TypeToken<Maybe<? extends String>>() {
    }.getType();
    assertThat(factory.get(sBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fBodyWildcard = new TypeToken<Flowable<? extends String>>() {
    }.getType();
    assertThat(factory.get(sBodyWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oBodyGeneric = new TypeToken<Observable<List<String>>>() {
    }.getType();
    assertThat(factory.get(oBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type sBodyGeneric = new TypeToken<Single<List<String>>>() {
    }.getType();
    assertThat(factory.get(sBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type mBodyGeneric = new TypeToken<Maybe<List<String>>>() {
    }.getType();
    assertThat(factory.get(sBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type fBodyGeneric = new TypeToken<Flowable<List<String>>>() {
    }.getType();
    assertThat(factory.get(sBodyGeneric, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(new TypeToken<List<String>>() {
    }.getType());
    Type oResponseClass = new TypeToken<Observable<Response<String>>>() {
    }.getType();
    assertThat(factory.get(oResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sResponseClass = new TypeToken<Single<Response<String>>>() {
    }.getType();
    assertThat(factory.get(sResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mResponseClass = new TypeToken<Maybe<Response<String>>>() {
    }.getType();
    assertThat(factory.get(sResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fResponseClass = new TypeToken<Flowable<Response<String>>>() {
    }.getType();
    assertThat(factory.get(sResponseClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oResponseWildcard = new TypeToken<Observable<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(oResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sResponseWildcard = new TypeToken<Single<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(sResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mResponseWildcard = new TypeToken<Maybe<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(sResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fResponseWildcard = new TypeToken<Flowable<Response<? extends String>>>() {
    }.getType();
    assertThat(factory.get(sResponseWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oResultClass = new TypeToken<Observable<Result<String>>>() {
    }.getType();
    assertThat(factory.get(oResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sResultClass = new TypeToken<Single<Result<String>>>() {
    }.getType();
    assertThat(factory.get(sResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mResultClass = new TypeToken<Maybe<Result<String>>>() {
    }.getType();
    assertThat(factory.get(sResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fResultClass = new TypeToken<Flowable<Result<String>>>() {
    }.getType();
    assertThat(factory.get(sResultClass, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type oResultWildcard = new TypeToken<Observable<Result<? extends String>>>() {
    }.getType();
    assertThat(factory.get(oResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type sResultWildcard = new TypeToken<Single<Result<? extends String>>>() {
    }.getType();
    assertThat(factory.get(sResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type mResultWildcard = new TypeToken<Maybe<Result<? extends String>>>() {
    }.getType();
    assertThat(factory.get(sResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
    Type fResultWildcard = new TypeToken<Flowable<Result<? extends String>>>() {
    }.getType();
    assertThat(factory.get(sResultWildcard, NO_ANNOTATIONS, retrofit).responseType()).isEqualTo(String.class);
}
Also used : Maybe(io.reactivex.Maybe) Observable(io.reactivex.Observable) Type(java.lang.reflect.Type) Single(io.reactivex.Single) TypeToken(com.google.common.reflect.TypeToken) Flowable(io.reactivex.Flowable) Test(org.junit.Test)

Aggregations

TypeToken (com.google.common.reflect.TypeToken)135 Test (org.junit.Test)60 HttpResponse (co.cask.common.http.HttpResponse)26 URL (java.net.URL)24 ServiceResponse (com.microsoft.rest.ServiceResponse)22 Response (retrofit2.Response)22 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)18 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)17 PipedInputStream (java.io.PipedInputStream)17 PipedOutputStream (java.io.PipedOutputStream)17 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)16 List (java.util.List)16 Map (java.util.Map)11 ImmutableList (com.google.common.collect.ImmutableList)9 Type (java.lang.reflect.Type)9 AbstractViewTest (org.corfudb.runtime.view.AbstractViewTest)9 NotFoundException (co.cask.cdap.common.NotFoundException)8 VirtualMachineScaleSetVMInstanceIDs (com.microsoft.azure.management.compute.VirtualMachineScaleSetVMInstanceIDs)8 Gson (com.google.gson.Gson)7 JsonObject (com.google.gson.JsonObject)7