Android Open Source - getrest Rest Service






From Project

Back to project page getrest.

License

The source code is released under:

Apache License

If you think the Android project getrest listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2012 Alexey Hanin//from w  w  w .j a  va2 s  . c o m
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package getrest.android.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import getrest.android.config.Config;
import getrest.android.config.ConfigResolver;
import getrest.android.core.Request;
import getrest.android.request.RequestContext;
import getrest.android.request.RequestManager;
import getrest.android.request.RequestStatus;
import getrest.android.util.Logger;
import getrest.android.util.LoggerFactory;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author aha
 * @since 2012-01-13
 */
public class RestService extends Service implements Broadcaster {

    private static final Logger LOGGER = LoggerFactory.getLogger("getrest.service");

    private final ThreadPoolExecutor jobExecutorService;

    public RestService() {
        this.jobExecutorService = new ThreadPoolExecutor(1, 5, 5, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new ThreadFactoryImpl());
    }

    @Override
    public int onStartCommand(final Intent intent, final int flags, final int startId) {
        final Config config = ConfigResolver.getInstance().obtainConfig(this);
        final Request request = new RequestWrapper(intent).getRequest();

        LOGGER.debug("Received request: requestId={0}, url={1}, method={2}. Submitting it to queue.",
                request.getRequestId(), request.getUri(), request.getMethod().getName());

        try {
            final RequestEventBus eventBus = new RequestEventBus();
            eventBus.setBroadcaster(this);

            eventBus.firePending(request.getRequestId());

            final RequestContext requestContext = config.getResourceContext(request.getUri()).getRequestContext(request);
            final RequestManager requestManager = requestContext.getRequestManager();
            requestManager.setRequestState(request.getRequestId(), RequestStatus.PENDING);

            final RequestJob job = new RequestJob(request);
            job.setRequestContext(requestContext);
            job.setRequestEventBus(eventBus);

            jobExecutorService.submit(job);
        } catch (RejectedExecutionException ex) {

            // TODO implement rejected execution fallback
            throw new UnsupportedOperationException("Handling of rejected execution exception is yet to implement");
        }

        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(final Intent intent) {
        return null;
    }

    @Override
    public void sendBroadcast(final Intent intent) {
        super.sendBroadcast(intent);
    }

    private static class ThreadFactoryImpl implements ThreadFactory {
        public Thread newThread(final Runnable runnable) {
            final Thread thread = new Thread(runnable);
            thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                public void uncaughtException(final Thread thread, final Throwable throwable) {
                    LOGGER.error("Uncaught exception in thread '{0}'", throwable, thread.getId());
                }
            });
            return thread;
        }
    }
}




Java Source Code List

getrest.android.RestfulClient.java
getrest.android.client.InMemoryRequestManager.java
getrest.android.client.RequestCallbackFactory.java
getrest.android.client.RequestCallback.java
getrest.android.client.RequestExecutor.java
getrest.android.client.RequestFuture.java
getrest.android.client.RequestRegistry.java
getrest.android.client.impl.RequestEventRecord.java
getrest.android.client.impl.RequestFutureImpl.java
getrest.android.client.impl.RequestRegistryEditorImpl.java
getrest.android.client.impl.RequestRegistryEntryFactory.java
getrest.android.client.impl.RequestRegistryPreferencesImpl.java
getrest.android.client.impl.RestfulClientImpl.java
getrest.android.client.impl.TransactionalRequestEntryStorage.java
getrest.android.config.ConfigResolver.java
getrest.android.config.Config.java
getrest.android.config.DefaultContributor.java
getrest.android.config.HasConfig.java
getrest.android.config.ResourceContextContribution.java
getrest.android.config.ResourceContextContributor.java
getrest.android.config.ResourceNode.java
getrest.android.config.ResourcePath.java
getrest.android.core.BaseRequest.java
getrest.android.core.ErrorState.java
getrest.android.core.Error.java
getrest.android.core.HandlerException.java
getrest.android.core.HasHeaders.java
getrest.android.core.Header.java
getrest.android.core.HeadersHelper.java
getrest.android.core.Headers.java
getrest.android.core.Method.java
getrest.android.core.Pack.java
getrest.android.core.Request.java
getrest.android.core.Response.java
getrest.android.core.Status.java
getrest.android.exception.GetrestException.java
getrest.android.exception.GetrestRuntimeException.java
getrest.android.executor.Handler.java
getrest.android.executor.PostMethodPipeline.java
getrest.android.executor.RequestHandlerFactory.java
getrest.android.executor.RequestHandlerImpl.java
getrest.android.executor.RequestHandler.java
getrest.android.executor.RequestPipeline.java
getrest.android.http.HttpEntityRepresentation.java
getrest.android.http.HttpServiceRequestExecutor.java
getrest.android.http.RepresentationHttpEntity.java
getrest.android.request.RequestContext.java
getrest.android.request.RequestLifecycle.java
getrest.android.request.RequestManager.java
getrest.android.request.RequestStatus.java
getrest.android.resource.Marshaller.java
getrest.android.resource.Packer.java
getrest.android.resource.ResourceContextImpl.java
getrest.android.resource.ResourceContext.java
getrest.android.service.Broadcaster.java
getrest.android.service.Representation.java
getrest.android.service.RequestEventBus.java
getrest.android.service.RequestJob.java
getrest.android.service.RequestStateChangeEventWrapper.java
getrest.android.service.RequestWrapper.java
getrest.android.service.RestService.java
getrest.android.service.ServiceRequestExecutor.java
getrest.android.service.ServiceRequest.java
getrest.android.service.ServiceResponse.java
getrest.android.testapp.GetrestTestApplication.java
getrest.android.testapp.MainActivity.java
getrest.android.util.LoggerFactory.java
getrest.android.util.Logger.java
getrest.android.util.WorkerQueue.java