@Override public Response run(JerseyResourceDelegateContext context) { JerseyResourceDelegateContextKey<String> sessionIdKey = JerseyResourceDelegateContextKey.valueOf(sessionIdKeyName, String.class); String sessionId = context.get(sessionIdKey); JerseyResourceDelegateContextKey<SubmitQueryRequest> submitQueryRequestKey = JerseyResourceDelegateContextKey.valueOf( submitQueryRequestKeyName, SubmitQueryRequest.class); SubmitQueryRequest request = context.get(submitQueryRequestKey); JerseyResourceDelegateContextKey<MasterContext> masterContextKey = JerseyResourceDelegateContextKey.valueOf( JerseyResourceDelegateUtil.MasterContextKey, MasterContext.class); MasterContext masterContext = context.get(masterContextKey); if (sessionId == null || sessionId.isEmpty()) { return ResourcesUtil.createBadRequestResponse(LOG, "Session Id is null or empty string."); } if (request == null || request.getQuery() == null || request.getQuery().isEmpty()) { return ResourcesUtil.createBadRequestResponse(LOG, "query is null or emptry string."); } Session session; try { session = masterContext.getSessionManager().getSession(sessionId); } catch (InvalidSessionException e) { return ResourcesUtil.createBadRequestResponse( LOG, "Provided session id (" + sessionId + ") is invalid."); } SubmitQueryResponse response = masterContext.getGlobalEngine().executeQuery(session, request.getQuery(), false); if (ReturnStateUtil.isError(response.getState())) { return ResourcesUtil.createExceptionResponse(LOG, response.getState().getMessage()); } else { JerseyResourceDelegateContextKey<UriInfo> uriInfoKey = JerseyResourceDelegateContextKey.valueOf( JerseyResourceDelegateUtil.UriInfoKey, UriInfo.class); UriInfo uriInfo = context.get(uriInfoKey); QueryId queryId = new QueryId(response.getQueryId()); URI queryURI = uriInfo .getBaseUriBuilder() .path(QueryResource.class) .path(QueryResource.class, "getQuery") .build(queryId.toString()); GetSubmitQueryResponse queryResponse = new GetSubmitQueryResponse(); if (queryId.isNull() == false) { queryResponse.setUri(queryURI); } queryResponse.setResultCode(response.getState().getReturnCode()); queryResponse.setQuery(request.getQuery()); return Response.status(Status.OK).entity(queryResponse).build(); } }
@GET @Path("/get1/{dd}") public void get(@Context UriInfo ui) { MultivaluedMap<String, String> queryParams = ui.getQueryParameters(); MultivaluedMap<String, String> pathParams = ui.getPathParameters(); for (Map.Entry<String, List<String>> entry : pathParams.entrySet()) { System.out.println( "path: key-" + entry.getKey() + " value-" + Arrays.toString(entry.getValue().toArray())); } for (Map.Entry<String, List<String>> entry : queryParams.entrySet()) { System.out.println( "query: key-" + entry.getKey() + " value-" + Arrays.toString(entry.getValue().toArray())); } }
@POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(BeeterMediaType.BEETER_AUTH_TOKEN) public Response registerUser( @FormParam("loginid") String loginid, @FormParam("password") String password, @FormParam("email") String email, @FormParam("fullname") String fullname, @Context UriInfo uriInfo) throws URISyntaxException { if (loginid == null || password == null || email == null || fullname == null) throw new BadRequestException("all parameters are mandatory"); UserDAO userDAO = new UserDAOImpl(); User user = null; AuthToken authToken = null; try { user = userDAO.createUser(loginid, password, email, fullname); authToken = (new AuthTokenDAOImpl()).createAuthToken(user.getId()); } catch (UserAlreadyExistsException e) { throw new WebApplicationException("loginid already exists", Response.Status.CONFLICT); } catch (SQLException e) { throw new InternalServerErrorException(); } URI uri = new URI(uriInfo.getAbsolutePath().toString() + "/" + user.getId()); return Response.created(uri).type(BeeterMediaType.BEETER_AUTH_TOKEN).entity(authToken).build(); }
@POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(GrouptalkMediaType.GROUPTALK_AUTH_TOKEN) public Response createJoinGroup( @FormParam("userid") String userid, @FormParam("groupid") String groupid, @Context UriInfo uriInfo) throws URISyntaxException { if (userid == null || groupid == null) throw new BadRequestException("all parameters are mandatory"); JoinGroupDAO joinGroupDAO = new JoinGroupDAOImpl(); JoinGroup joinGroup = null; AuthToken authenticationToken = null; try { joinGroup = joinGroupDAO.createJoinGroup(securityContext.getUserPrincipal().getName(), groupid); } catch (SQLException e) { throw new InternalServerErrorException(); } URI uri = new URI(uriInfo.getAbsolutePath().toString() + "/" + joinGroup.getUserid()); return Response.created(uri) .type(GrouptalkMediaType.GROUPTALK_InterestGroups) .entity(joinGroup) .build(); }
@PUT @Consumes(MediaType.APPLICATION_XML) public Response putTaskData(String value) { Response res = null; // add your code here // first check if the Entity exists in the datastore DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService(); syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO)); Key entKey = KeyFactory.createKey("TaskData", keyname); Date date = new Date(); try { // if it is, signal that we updated the entity Entity ts = datastore.get(entKey); ts.setProperty("value", value); ts.setProperty("date", date); datastore.put(ts); res = Response.noContent().build(); } catch (EntityNotFoundException e) { // if it is not, create it and // signal that we created the entity in the datastore Entity taskdata = new Entity("TaskData", keyname); taskdata.setProperty("value", value); taskdata.setProperty("date", date); datastore.put(taskdata); res = Response.created(uriInfo.getAbsolutePath()).build(); } TaskData td = new TaskData(keyname, value, date); syncCache.put(keyname, td); return res; }
@Path(("/registrar")) @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MusicloudMediaType.MUSICLOUD_AUTH_TOKEN) public Response registerUser( @FormParam("login") String login, @FormParam("nombre") String nombre, @FormParam("apellidos") String apellidos, @FormParam("email") String email, @FormParam("password") String password, @Context UriInfo uriInfo) throws URISyntaxException { if (login == null || nombre == null || apellidos == null || password == null || email == null) throw new BadRequestException("Se necesitan todos los parametros"); UserDAO userDAO = new UserDAOImpl(); User user = null; try { user = userDAO.crear_usuario_registrado(login, nombre, apellidos, email, password); } catch (UserAlreadyExistsException e) { throw new WebApplicationException("Este login ya existe ", Response.Status.CONFLICT); } catch (SQLException e) { throw new InternalServerErrorException(); } URI uri = new URI(uriInfo.getAbsolutePath().toString() + "/" + user.getId()); return Response.ok().build(); }