text:
package com.intive.intivemvpframework.domain.interactor;

import com.intive.intivemvpframework.domain.repository.UserRepository;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;

public class GetUserListUseCaseTest {

    private GetUserListUseCase mGetUserListUseCase;

    @Mock
    private UserRepository mockUserRepository;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mGetUserListUseCase = new GetUserListUseCase(mockUserRepository);
    }

    @Test
    public void testGetUserListUseCaseObservableHappyCase() {
        mGetUserListUseCase.buildUseCaseObservable();

        verify(mockUserRepository).users();
        verifyNoMoreInteractions(mockUserRepository);
    }
}


text:
package co.cask.cdap.common.http;

import co.cask.cdap.common.conf.Constants;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunk;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * An UpstreamHandler that verifies the userId in a request header and updates the {@code SecurityRequestContext}.
 */
public class AuthenticationChannelHandler extends SimpleChannelUpstreamHandler {
  private static final Logger LOG = LoggerFactory.getLogger(AuthenticationChannelHandler.class);

  private String currentUserId;
  private String currentUserIP;

  /**
   * Decode the AccessTokenIdentifier passed as a header and set it in a ThreadLocal.
   * Returns a 401 if the identifier is malformed.
   */
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object message = e.getMessage();
    if (message instanceof HttpRequest) {
      // TODO: authenticate the user using user id - CDAP-688
      HttpRequest request = (HttpRequest) message;
      currentUserId = request.getHeader(Constants.Security.Headers.USER_ID);
      currentUserIP = request.getHeader(Constants.Security.Headers.USER_IP);
      SecurityRequestContext.setUserId(currentUserId);
      SecurityRequestContext.setUserIP(currentUserIP);
    } else if (message instanceof HttpChunk) {
      SecurityRequestContext.setUserId(currentUserId);
      SecurityRequestContext.setUserIP(currentUserIP);
    }

    super.messageReceived(ctx, e);
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
    LOG.error("Got exception: ", e.getCause());
    ChannelFuture future = Channels.future(ctx.getChannel());
    future.addListener(ChannelFutureListener.CLOSE);
    // TODO: add WWW-Authenticate header for 401 response -  REACTOR-900
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.UNAUTHORIZED);
    Channels.write(ctx, future, response);
  }
}

redpajama_set_name: RedPajamaGithub


text:
package http

import (
	"context"

	"github.com/go-kit/kit/endpoint"
	"github.com/mainflux/mainflux/things"
)

func identifyEndpoint(svc things.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(identifyReq)
		if err := req.validate(); err != nil {
			return nil, err
		}

		id, err := svc.Identify(ctx, req.Token)
		if err != nil {
			return nil, err
		}

		res := identityRes{
			ID: id,
		}

		return res, nil
	}
}

func canAccessByKeyEndpoint(svc things.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(canAccessByKeyReq)
		if err := req.validate(); err != nil {
			return nil, err
		}

		id, err := svc.CanAccessByKey(ctx, req.chanID, req.Token)
		if err != nil {
			return nil, err
		}

		res := identityRes{
			ID: id,
		}

		return res, nil
	}
}

func canAccessByIDEndpoint(svc things.Service) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(canAccessByIDReq)
		if err := req.validate(); err != nil {
			return nil, err
		}

		if err := svc.CanAccessByID(ctx, req.chanID, req.ThingID); err != nil {
			return nil, err
		}

		res := canAccessByIDRes{}
		return res, nil
	}
}


text:
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/ppb_image_data_proxy.h"

#include <string.h>  // For memcpy

#include <map>
#include <vector>

#include "base/bind.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/singleton.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/proxy/enter_proxy.h"
#include "ppapi/proxy/host_dispatcher.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/plugin_globals.h"
#include "ppapi/proxy/plugin_resource_tracker.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/shared_impl/host_resource.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/scoped_pp_resource.h"
#include "ppapi/thunk/enter.h"
#include "ppapi/thunk/thunk.h"

#if !defined(OS_NACL)
#include "skia/ext/platform_canvas.h"
#include "ui/surface/transport_dib.h"
#endif

using ppapi::thunk::PPB_ImageData_API;

namespace ppapi {
namespace proxy {

namespace {

// How ImageData re-use works
// --------------------------
//
// When animating plugins (like video), re-creating image datas for each frame
// and mapping the memory has a high overhead. So we try to re-use these when
// possible.
//
// 1. Plugin makes an asynchronous call that transfers an ImageData to the
//    implementation of some API.
// 2. Plugin frees its ImageData reference. If it doesn't do this we can't
//    re-use it.
// 3. When the last plugin ref of an ImageData is released, we don't actually
//    delete it. Instead we put it on a queue where we hold onto it in the
//    plugin process for a short period of time.
// 4. The API implementation that received the ImageData finishes using it.
//    Without our caching system it would get deleted at this point.
// 5. The proxy in the renderer will send NotifyUnusedImageData back to the
//    plugin process. We check if the given resource is in the queue and mark
//    it as usable.
// 6. When the plugin requests a new image data, we check our queue and if there
//    is a usable ImageData of the right size and format, we'll return it
//    instead of making a new one. It's important that caching is only requested
//    when the size is unlikely to change, so cache hits are high.
//
// Some notes:
//
//  - We only re-use image data when the plugin and host are rapidly exchanging
//    them and the size is likely to remain constant. It should be clear that
//    the plugin is promising that it's done with the image.
//
//  - Theoretically we could re-use them in other cases but the lifetime
//    becomes more difficult to manage. The plugin could have used an ImageData
//    in an arbitrary number of queued up PaintImageData calls which we would
//    have to check.
//
//  - If a flush takes a long time or there are many released image datas
//    accumulating in our queue such that some are deleted, we will have
//    released our reference by the time the renderer notifies us of an unused
//    image data. In this case we just give up.
//
//  - We maintain a per-instance cache. Some pages have many instances of
//    Flash, for example, each of a different size. If they're all animating we
//    want each to get its own image data re-use.
//
//  - We generate new resource IDs when re-use happens to try to avoid weird
//    problems if the plugin messes up its refcounting.

// Keep a cache entry for this many seconds before expiring it. We get an entry
// back from the renderer after an ImageData is swapped out, so it means the
// plugin has to be painting at least two frames for this time interval to
// get caching.
static const int kMaxAgeSeconds = 2;

// ImageDataCacheEntry ---------------------------------------------------------

struct ImageDataCacheEntry {
  ImageDataCacheEntry() : usable(false) {}
  explicit ImageDataCacheEntry(ImageData* i)
      : added_time(base::TimeTicks::Now()), usable(false), image(i) {}

  base::TimeTicks added_time;

  // Set to true when the renderer tells us that it's OK to re-use this image.
  bool usable;

  scoped_refptr<ImageData> image;
};

// ImageDataInstanceCache ------------------------------------------------------

// Per-instance cache of image datas.
class ImageDataInstanceCache {
 public:
  ImageDataInstanceCache() : next_insertion_point_(0) {}

  // These functions have the same spec as the ones in ImageDataCache.
  scoped_refptr<ImageData> Get(PPB_ImageData_Shared::ImageDataType type,
                               int width, int height,
                               PP_ImageDataFormat format);
  void Add(ImageData* image_data);
  void ImageDataUsable(ImageData* image_data);

  // Expires old entries. Returns true if there are still entries in the list,
  // false if this instance cache is now empty.
  bool ExpireEntries();

 private:
  void IncrementInsertionPoint();

  // We'll store this many ImageDatas per instance.
  static const size_t kCacheSize = 2;

  ImageDataCacheEntry images_[kCacheSize];

  // Index into cache where the next item will go.
  size_t next_insertion_point_;
};

scoped_refptr<ImageData> ImageDataInstanceCache::Get(
    PPB_ImageData_Shared::ImageDataType type,
    int width, int height,
    PP_ImageDataFormat format) {
  // Just do a brute-force search since the cache is so small.
  for (size_t i = 0; i < kCacheSize; i++) {
    if (!images_[i].usable)
      continue;
    if (images_[i].image->type() != type)
      continue;
    const PP_ImageDataDesc& desc = images_[i].image->desc();
    if (desc.format == format &&
        desc.size.width == width && desc.size.height == height) {
      scoped_refptr<ImageData> ret(images_[i].image);
      images_[i] = ImageDataCacheEntry();

      // Since we just removed an item, this entry is the best place to insert
      // a subsequent one.
      next_insertion_point_ = i;
      return ret;
    }
  }
  return scoped_refptr<ImageData>();
}

void ImageDataInstanceCache::Add(ImageData* image_data) {
  images_[next_insertion_point_] = ImageDataCacheEntry(image_data);
  IncrementInsertionPoint();
}

void ImageDataInstanceCache::ImageDataUsable(ImageData* image_data) {
  for (size_t i = 0; i < kCacheSize; i++) {
    if (images_[i].image.get() == image_data) {
      images_[i].usable = true;

      // This test is important. The renderer doesn't guarantee how many image
      // datas it has or when it notifies us when one is usable. Its possible
      // to get into situations where it's always telling us the old one is
      // usable, and then the older one immediately gets expired. Therefore,
      // if the next insertion would overwrite this now-usable entry, make the
      // next insertion overwrite some other entry to avoid the replacement.
      if (next_insertion_point_ == i)
        IncrementInsertionPoint();
      return;
    }
  }
}

bool ImageDataInstanceCache::ExpireEntries() {
  base::TimeTicks threshold_time =
      base::TimeTicks::Now() - base::TimeDelta::FromSeconds(kMaxAgeSeconds);

  bool has_entry = false;
  for (size_t i = 0; i < kCacheSize; i++) {
    if (images_[i].image.get()) {
      // Entry present.
      if (images_[i].added_time <= threshold_time) {
        // Found an entry to expire.
        images_[i] = ImageDataCacheEntry();
        next_insertion_point_ = i;
      } else {
        // Found an entry that we're keeping.
        has_entry = true;
      }
    }
  }
  return has_entry;
}

void ImageDataInstanceCache::IncrementInsertionPoint() {
  // Go to the next location, wrapping around to get LRU.
  next_insertion_point_++;
  if (next_insertion_point_ >= kCacheSize)
    next_insertion_point_ = 0;
}

// ImageDataCache --------------------------------------------------------------

class ImageDataCache {
 public:
  ImageDataCache() {}
  ~ImageDataCache() {}

  static ImageDataCache* GetInstance();

  // Retrieves an image data from the cache of the specified type, size and
  // format if one exists. If one doesn't exist, this will return a null refptr.
  scoped_refptr<ImageData> Get(PP_Instance instance,
                               PPB_ImageData_Shared::ImageDataType type,
                               int width, int height,
                               PP_ImageDataFormat format);

  // Adds the given image data to the cache. There should be no plugin
  // references to it. This may delete an older item from the cache.
  void Add(ImageData* image_data);

  // Notification from the renderer that the given image data is usable.
  void ImageDataUsable(ImageData* image_data);

  void DidDeleteInstance(PP_Instance instance);

 private:
  friend struct base::LeakySingletonTraits<ImageDataCache>;

  // Timer callback to expire entries for the given instance.
  void OnTimer(PP_Instance instance);

  typedef std::map<PP_Instance, ImageDataInstanceCache> CacheMap;
  CacheMap cache_;

  // This class does timer calls and we don't want to run these outside of the
  // scope of the object. Technically, since this class is a leaked static,
  // this will never happen and this factory is unnecessary. However, it's
  // probably better not to make assumptions about the lifetime of this class.
  base::WeakPtrFactory<ImageDataCache> weak_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(ImageDataCache);
};

// static
ImageDataCache* ImageDataCache::GetInstance() {
  return base::Singleton<ImageDataCache,
                         base::LeakySingletonTraits<ImageDataCache>>::get();
}

scoped_refptr<ImageData> ImageDataCache::Get(
    PP_Instance instance,
    PPB_ImageData_Shared::ImageDataType type,
    int width, int height,
    PP_ImageDataFormat format) {
  CacheMap::iterator found = cache_.find(instance);
  if (found == cache_.end())
    return scoped_refptr<ImageData>();
  return found->second.Get(type, width, height, format);
}

void ImageDataCache::Add(ImageData* image_data) {
  cache_[image_data->pp_instance()].Add(image_data);

  // Schedule a timer to invalidate this entry.
  PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostDelayedTask(
      FROM_HERE,
      RunWhileLocked(base::BindOnce(&ImageDataCache::OnTimer,
                                    weak_factory_.GetWeakPtr(),
                                    image_data->pp_instance())),
      base::TimeDelta::FromSeconds(kMaxAgeSeconds));
}

void ImageDataCache::ImageDataUsable(ImageData* image_data) {
  CacheMap::iterator found = cache_.find(image_data->pp_instance());
  if (found != cache_.end())
    found->second.ImageDataUsable(image_data);
}

void ImageDataCache::DidDeleteInstance(PP_Instance instance) {
  cache_.erase(instance);
}

void ImageDataCache::OnTimer(PP_Instance instance) {
  CacheMap::iterator found = cache_.find(instance);
  if (found == cache_.end())
    return;
  if (!found->second.ExpireEntries()) {
    // There are no more entries for this instance, remove it from the cache.
    cache_.erase(found);
  }
}

}  // namespace

// ImageData -------------------------------------------------------------------

ImageData::ImageData(const HostResource& resource,
                     PPB_ImageData_Shared::ImageDataType type,
                     const PP_ImageDataDesc& desc)
    : Resource(OBJECT_IS_PROXY, resource),
      type_(type),
      desc_(desc),
      is_candidate_for_reuse_(false) {
}

ImageData::~ImageData() {
}

PPB_ImageData_API* ImageData::AsPPB_ImageData_API() {
  return this;
}

void ImageData::LastPluginRefWasDeleted() {
  // The plugin no longer needs this ImageData, add it to our cache if it's
  // been used in a ReplaceContents. These are the ImageDatas that the renderer
  // will send back ImageDataUsable messages for.
  if (is_candidate_for_reuse_)
    ImageDataCache::GetInstance()->Add(this);
}

void ImageData::InstanceWasDeleted() {
  ImageDataCache::GetInstance()->DidDeleteInstance(pp_instance());
}

PP_Bool ImageData::Describe(PP_ImageDataDesc* desc) {
  memcpy(desc, &desc_, sizeof(PP_ImageDataDesc));
  return PP_TRUE;
}

int32_t ImageData::GetSharedMemoryRegion(
    base::UnsafeSharedMemoryRegion** /* region */) {
  // Not supported in the proxy (this method is for actually implementing the
  // proxy in the host).
  return PP_ERROR_NOACCESS;
}

void ImageData::SetIsCandidateForReuse() {
  is_candidate_for_reuse_ = true;
}

void ImageData::RecycleToPlugin(bool zero_contents) {
  is_candidate_for_reuse_ = false;
  if (zero_contents) {
    void* data = Map();
    memset(data, 0, desc_.stride * desc_.size.height);
    Unmap();
  }
}

// PlatformImageData -----------------------------------------------------------

#if !defined(OS_NACL)
PlatformImageData::PlatformImageData(
    const HostResource& resource,
    const PP_ImageDataDesc& desc,
    base::UnsafeSharedMemoryRegion image_region)
    : ImageData(resource, PPB_ImageData_Shared::PLATFORM, desc) {
#if defined(OS_WIN)
  transport_dib_ = TransportDIB::CreateWithHandle(std::move(image_region));
#else
  transport_dib_ = TransportDIB::Map(std::move(image_region));
#endif  // defined(OS_WIN)
}

PlatformImageData::~PlatformImageData() = default;

void* PlatformImageData::Map() {
  if (!mapped_canvas_.get()) {
    if (!transport_dib_.get())
      return nullptr;

    const bool is_opaque = false;
    mapped_canvas_ = transport_dib_->GetPlatformCanvas(
        desc_.size.width, desc_.size.height, is_opaque);
    if (!mapped_canvas_.get())
      return nullptr;
  }
  SkPixmap pixmap;
  skia::GetWritablePixels(mapped_canvas_.get(), &pixmap);
  return pixmap.writable_addr();
}

void PlatformImageData::Unmap() {
  // TODO(brettw) have a way to unmap a TransportDIB. Currently this isn't
  // possible since deleting the TransportDIB also frees all the handles.
  // We need to add a method to TransportDIB to release the handles.
}

SkCanvas* PlatformImageData::GetCanvas() {
  return mapped_canvas_.get();
}
#endif  // !defined(OS_NACL)

// SimpleImageData -------------------------------------------------------------

SimpleImageData::SimpleImageData(const HostResource& resource,
                                 const PP_ImageDataDesc& desc,
                                 base::UnsafeSharedMemoryRegion region)
    : ImageData(resource, PPB_ImageData_Shared::SIMPLE, desc),
      shm_region_(std::move(region)),
      size_(desc.size.width * desc.size.height * 4),
      map_count_(0) {}

SimpleImageData::~SimpleImageData() = default;

void* SimpleImageData::Map() {
  if (map_count_++ == 0)
    shm_mapping_ = shm_region_.MapAt(0, size_);
  return shm_mapping_.IsValid() ? shm_mapping_.memory() : nullptr;
}

void SimpleImageData::Unmap() {
  if (--map_count_ == 0)
    shm_mapping_ = base::WritableSharedMemoryMapping();
}

SkCanvas* SimpleImageData::GetCanvas() {
  return nullptr;  // No canvas available.
}

// PPB_ImageData_Proxy ---------------------------------------------------------

PPB_ImageData_Proxy::PPB_ImageData_Proxy(Dispatcher* dispatcher)
    : InterfaceProxy(dispatcher) {
}

PPB_ImageData_Proxy::~PPB_ImageData_Proxy() {
}

// static
PP_Resource PPB_ImageData_Proxy::CreateProxyResource(
    PP_Instance instance,
    PPB_ImageData_Shared::ImageDataType type,
    PP_ImageDataFormat format,
    const PP_Size& size,
    PP_Bool init_to_zero) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return 0;

  // Check the cache.
  scoped_refptr<ImageData> cached_image_data =
      ImageDataCache::GetInstance()->Get(instance, type,
                                         size.width, size.height, format);
  if (cached_image_data.get()) {
    // We have one we can re-use rather than allocating a new one.
    cached_image_data->RecycleToPlugin(PP_ToBool(init_to_zero));
    return cached_image_data->GetReference();
  }

  HostResource result;
  PP_ImageDataDesc desc;
  switch (type) {
    case PPB_ImageData_Shared::SIMPLE: {
      ppapi::proxy::SerializedHandle image_handle;
      dispatcher->Send(new PpapiHostMsg_PPBImageData_CreateSimple(
          kApiID, instance, format, size, init_to_zero, &result, &desc,
          &image_handle));
      if (image_handle.is_shmem_region()) {
        base::UnsafeSharedMemoryRegion image_region =
            base::UnsafeSharedMemoryRegion::Deserialize(
                image_handle.TakeSharedMemoryRegion());
        if (!result.is_null()) {
          return (new SimpleImageData(result, desc, std::move(image_region)))
              ->GetReference();
        }
      }
      break;
    }
    case PPB_ImageData_Shared::PLATFORM: {
#if !defined(OS_NACL)
      ppapi::proxy::SerializedHandle image_handle;
      dispatcher->Send(new PpapiHostMsg_PPBImageData_CreatePlatform(
          kApiID, instance, format, size, init_to_zero, &result, &desc,
          &image_handle));
      if (image_handle.is_shmem_region()) {
        base::UnsafeSharedMemoryRegion image_region =
            base::UnsafeSharedMemoryRegion::Deserialize(
                image_handle.TakeSharedMemoryRegion());
        if (!result.is_null()) {
          return (new PlatformImageData(result, desc, std::move(image_region)))
              ->GetReference();
        }
      }
#else
      // PlatformImageData shouldn't be created in untrusted code.
      NOTREACHED();
#endif
      break;
    }
  }

  return 0;
}

bool PPB_ImageData_Proxy::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(PPB_ImageData_Proxy, msg)
#if !defined(OS_NACL)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_CreatePlatform,
                        OnHostMsgCreatePlatform)
    IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBImageData_CreateSimple,
                        OnHostMsgCreateSimple)
#endif
    IPC_MESSAGE_HANDLER(PpapiMsg_PPBImageData_NotifyUnusedImageData,
                        OnPluginMsgNotifyUnusedImageData)

    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

#if !defined(OS_NACL)
// static
PP_Resource PPB_ImageData_Proxy::CreateImageData(
    PP_Instance instance,
    PPB_ImageData_Shared::ImageDataType type,
    PP_ImageDataFormat format,
    const PP_Size& size,
    bool init_to_zero,
    PP_ImageDataDesc* desc,
    base::UnsafeSharedMemoryRegion* image_region) {
  HostDispatcher* dispatcher = HostDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return 0;

  thunk::EnterResourceCreation enter(instance);
  if (enter.failed())
    return 0;

  PP_Bool pp_init_to_zero = init_to_zero ? PP_TRUE : PP_FALSE;
  PP_Resource pp_resource = 0;
  switch (type) {
    case PPB_ImageData_Shared::SIMPLE: {
      pp_resource = enter.functions()->CreateImageDataSimple(
          instance, format, &size, pp_init_to_zero);
      break;
    }
    case PPB_ImageData_Shared::PLATFORM: {
      pp_resource = enter.functions()->CreateImageData(
          instance, format, &size, pp_init_to_zero);
      break;
    }
  }

  if (!pp_resource)
    return 0;

  ppapi::ScopedPPResource resource(ppapi::ScopedPPResource::PassRef(),
                                   pp_resource);

  thunk::EnterResourceNoLock<PPB_ImageData_API> enter_resource(resource.get(),
                                                               false);
  if (enter_resource.object()->Describe(desc) != PP_TRUE) {
    DVLOG(1) << "CreateImageData failed: could not Describe";
    return 0;
  }

  base::UnsafeSharedMemoryRegion* local_shm;
  if (enter_resource.object()->GetSharedMemoryRegion(&local_shm) != PP_OK) {
    DVLOG(1) << "CreateImageData failed: could not GetSharedMemory";
    return 0;
  }

  *image_region =
      dispatcher->ShareUnsafeSharedMemoryRegionWithRemote(*local_shm);
  return resource.Release();
}

void PPB_ImageData_Proxy::OnHostMsgCreatePlatform(
    PP_Instance instance,
    int32_t format,
    const PP_Size& size,
    PP_Bool init_to_zero,
    HostResource* result,
    PP_ImageDataDesc* desc,
    ppapi::proxy::SerializedHandle* result_image_handle) {
  // Clear |desc| so we don't send uninitialized memory to the plugin.
  // https://crbug.com/391023.
  *desc = PP_ImageDataDesc();
  base::UnsafeSharedMemoryRegion image_region;
  PP_Resource resource =
      CreateImageData(instance, PPB_ImageData_Shared::PLATFORM,
                      static_cast<PP_ImageDataFormat>(format), size,
                      true /* init_to_zero */, desc, &image_region);
  result->SetHostResource(instance, resource);
  if (resource) {
    result_image_handle->set_shmem_region(
        base::UnsafeSharedMemoryRegion::TakeHandleForSerialization(
            std::move(image_region)));
  } else {
    result_image_handle->set_null_shmem_region();
  }
}

void PPB_ImageData_Proxy::OnHostMsgCreateSimple(
    PP_Instance instance,
    int32_t format,
    const PP_Size& size,
    PP_Bool init_to_zero,
    HostResource* result,
    PP_ImageDataDesc* desc,
    ppapi::proxy::SerializedHandle* result_image_handle) {
  // Clear |desc| so we don't send uninitialized memory to the plugin.
  // https://crbug.com/391023.
  *desc = PP_ImageDataDesc();
  base::UnsafeSharedMemoryRegion image_region;
  PP_Resource resource =
      CreateImageData(instance, PPB_ImageData_Shared::SIMPLE,
                      static_cast<PP_ImageDataFormat>(format), size,
                      true /* init_to_zero */, desc, &image_region);
  result->SetHostResource(instance, resource);
  if (resource) {
    result_image_handle->set_shmem_region(
        base::UnsafeSharedMemoryRegion::TakeHandleForSerialization(
            std::move(image_region)));
  } else {
    result_image_handle->set_null_shmem_region();
  }
}
#endif  // !defined(OS_NACL)

void PPB_ImageData_Proxy::OnPluginMsgNotifyUnusedImageData(
    const HostResource& old_image_data) {
  PluginGlobals* plugin_globals = PluginGlobals::Get();
  if (!plugin_globals) {
    return;  // This may happen if the plugin is maliciously sending this
             // message to the renderer.
  }

  EnterPluginFromHostResource<PPB_ImageData_API> enter(old_image_data);
  if (enter.succeeded()) {
    ImageData* image_data = static_cast<ImageData*>(enter.object());
    ImageDataCache::GetInstance()->ImageDataUsable(image_data);
  }

  // The renderer sent us a reference with the message. If the image data was
  // still cached in our process, the proxy still holds a reference so we can
  // remove the one the renderer just sent is. If the proxy no longer holds a
  // reference, we released everything and we should also release the one the
  // renderer just sent us.
  dispatcher()->Send(new PpapiHostMsg_PPBCore_ReleaseResource(
      API_ID_PPB_CORE, old_image_data));
}

}  // namespace proxy
}  // namespace ppapi


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HashTableNamespace
{
    public class Hash
    {
        private static int N = 1000;
        private List[] hash = new List[N];
        private IHashFunction hashF;
        /// <summary>
        /// constructor for hashtable
        /// </summary>
        public Hash(IHashFunction getHashFunction)
        {
            hashF = getHashFunction;
            for (int i = 0; i < N; ++i)
            {
                hash[i] = new List();
            }
        }

        /// <summary>
        /// add element in hashtable
        /// </summary>
        /// <param name="element"></param>
        public void Add(string element)
        {
            hash[hashF.HashFunction(element)].Push(element);
        }

        /// <summary>
        /// delete element from hashtable
        /// </summary>
        /// <param name="element"></param>
        public bool Delete(string element)
        {
            return (hash[hashF.HashFunction(element)].Pop(element) != "");
        }

        /// <summary>
        /// search element in hashtable
        /// </summary>
        /// <param name="element"></param>
        public bool Search(string element)
        {
            return (hash[hashF.HashFunction(element)].Find(element));
        }

        /// <summary>
        /// priant all elements
        /// </summary>
        public void Print()
        {
            for (int i = 0; i < N; ++i)
            {
                if (hash[i].HaveElements())
                {
                    hash[i].Print();
                    Console.WriteLine();
                }
            }
        }
    }
}


text:
package com.kokaba.rxfirebase.helpers;

public class TestingConstants {

    public static final int NUMBER_OF_ELEMENTS = 6;
    public static final String FIXED_TESTING_DATA_KEY = "testing_data";
    public static final String DYNAMIC_TESTING_DATA_KEY = "testing_zone";

    public static final SampleDataModel SAMPLE_DATA_MODEL = new SampleDataModel("Name", "Surname");
    public static final SampleDataModel SAMPLE_DATA_MODEL2 = new SampleDataModel("Name2", "Surname2");

}


text:
#include <QtGui>

#include "stylewindow.h"

//! [0]
int main(int argv, char *args[])
{
    QApplication app(argv, args);
    QApplication::setStyle(QStyleFactory::create("simplestyle"));

    StyleWindow window;
    window.resize(200, 50);
    window.show();

    return app.exec();
}
//! [0]


text:
package com.example.yaowu.steamhelper;

import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yaowu.steamhelper.Adapter.FriendsAdapter;
import com.example.yaowu.steamhelper.db.Friend;
import com.example.yaowu.steamhelper.db.UserInfo;
import com.example.yaowu.steamhelper.util.HttpUtil;
import com.example.yaowu.steamhelper.util.NavigationIniti;
import com.example.yaowu.steamhelper.util.Utility;

import org.litepal.crud.DataSupport;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class FriendsActivity extends AppCompatActivity {

    private FriendsAdapter friendsAdapter;
    private List<Friend> friendList;

    private ListView friend_list_view;
    private NavigationView navigationView;
    private DrawerLayout friends_drawerLayout;
    private TextView titletext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friend_list);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //set navigation menu
        friends_drawerLayout = (DrawerLayout)findViewById(R.id.friend_drawer_layout);
        //set navigation button
        toolbar.setNavigationIcon(R.drawable.castlecrahsers);
        getSupportActionBar().setTitle("Friends");

        //load navi bar
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        NavigationIniti.initiNavView(navigationView, this, friends_drawerLayout);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                friends_drawerLayout.openDrawer(GravityCompat.START);
            }
        });
        //initi list view
        friend_list_view = (ListView) findViewById(R.id.friend_listview);

        queryFriends();
    }

    private void queryFriends(){
        //find friends
        friendList = DataSupport.findAll(Friend.class);

        if(friendList.size() == 0 || friendList.get(0).getFriendinfo() == null){
            String address = "http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=" + GameListFragment.PERSONAL_KEY
                    + "&steamid=" + GameListFragment.STEAM_ID + "&relationship=friend";
            queryFriendFromServer(address);

        }else{

            Log.d("friends size", String.valueOf(friendList.size()));
            friendsAdapter = new FriendsAdapter(FriendsActivity.this, R.layout.friend_item, friendList);
            friend_list_view.setAdapter(friendsAdapter);
            friend_list_view.setSelection(0);
        }
    }

    private void queryFriendFromServer(String address){
        HttpUtil.sendOkHttpRequest(address, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Toast.makeText(FriendsActivity.this,"Failed to load friends list",Toast.LENGTH_SHORT);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseText = response.body().string();
                boolean result = Utility.handleFriendListResponse(responseText);

                if(result){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("query:", " friends summary");
                            queryFriendsSummaryFromServer();
                        }
                    });
                }
            }
        });
    }

    /**
     * query friends summaries from server
     */
    private void queryFriendsSummaryFromServer(){

        friendList = DataSupport.findAll(Friend.class);

        //get address
        StringBuilder friendsSummary = new StringBuilder( "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key="
                + GameListFragment.PERSONAL_KEY
                + "&steamids=");

        for(int i = 0; i < friendList.size()-1; i++){
            friendsSummary.append(friendList.get(i).getFriendsteamid() + "||steamids=");
        }
        friendsSummary.append(friendList.get(friendList.size()-1).getFriendsteamid());
        friendsSummary.append("&relationship=friend");

        Log.d("friends summaries", friendsSummary.toString());
        HttpUtil.sendOkHttpRequest(friendsSummary.toString(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(FriendsActivity.this, "Failed to get friends summary", Toast.LENGTH_SHORT).show();

                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseText = response.body().string();
                boolean result = Utility.handleUserInfo(responseText);
                if(result){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            queryFriends();//these two operations will be done by two threads, so it will perform twice;
                        }
                    });
                }

            }
        });

    }
}


text:
project_path: /web/_project.yaml
book_path: /web/ilt/pwa/_book.yaml

{# wf_auto_generated #}
{# wf_updated_on: 2017-04-10T05:16:46Z #}
{# wf_published_on: 2016-01-01 #}

# Lab: Scripting the Service Worker {: .page-title }

Concepts:  [Introduction to Service Worker](introduction-to-service-worker)

## Overview

This lab walks you through creating a simple service worker.

#### What you will learn

* Create a basic service worker script, install it, and do simple debugging

#### What you should know

* Basic JavaScript and HTML
* Concepts and basic syntax of ES2015  [Promises](/web/fundamentals/getting-started/primers/promises)
* Concept of an  [Immediately Invoked Function Expression](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression) (IIFE)
* How to enable the developer console

#### What you need before you begin

* Computer with terminal/shell access
* Connection to the internet
* A  [browser that supports service workers](https://jakearchibald.github.io/isserviceworkerready/)
* A text editor

## 1. Get set up

If you have not downloaded the repository, installed Node, and started a local server, follow the instructions in [Setting up the labs](setting-up-the-labs).

Open your browser and navigate to __localhost:8080/service-worker-lab/app__.

Note: If you have installed a service worker on localhost before, unregister it so that it doesn't interfere with the lab.

If you have a text editor that lets you open a project, open the __service-worker/app__ folder. This will make it easier to stay organized. Otherwise, open the folder in your computer's file system. The __app__ folder is where you will be building the lab.

This folder contains:

* __other.html__, __js/other.js__, __below/another.html__, and __js/another.js__ are sample resources that we use to experiment
* __index.html__ is the main HTML page for our sample site/application
* __index.css__ is the cascading stylesheet for __index.html__
* __service-worker.js__ is the JavaScript file that is used to create our service worker
* __styles__ folder contains the cascading stylesheets for this lab
* __test__ folder contains files for testing your progress

## 2. Register the service worker

Open __service-worker.js__ in your text editor. Note that the file contains only an empty function. We have not added any code to run within the service worker yet.

Note: We are using an Immediately Invoked Function Expression inside the service worker. This is just a best practice for avoiding namespace pollution; it is not related to the Service Worker API.

Open __index.html__ in your text editor.

Replace TODO 2 with the following code:

#### index.html

```
if (!('serviceWorker' in navigator)) {
  console.log('Service worker not supported');
  return;
}
navigator.serviceWorker.register('service-worker.js')
.then(function() {
  console.log('Registered');
})
.catch(function(error) {
  console.log('Registration failed:', error);
});
```

Save the script and refresh the page. The console should return a message indicating that the service worker was registered.

In your browser, navigate to __test-registered.html__ (__app/test/test-registered.html__) to confirm that you have registered the service worker. This is a unit test. Passed tests are blue and failed tests are red. If you've done everything correctly so far, this test should be blue. Close the test page when you are done with it.

Note: Be sure to open the test page using the localhost address so that it opens from the server and not directly from the file system.

__Optional__: Open the site on an  [unsupported browser](https://jakearchibald.github.io/isserviceworkerready/) and verify that the support check conditional works.

#### Explanation

Service workers must be registered. Always begin by checking whether the browser supports service workers. The service worker is exposed on the window's  [`Navigator`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator) object and can be accessed with `window.navigator.serviceWorker`.

In our code, if service workers aren't supported, the script logs a message and fails immediately. Calling `serviceworker.register(...)` registers the service worker, installing the service worker's script. This returns a promise that resolves once the service worker is successfully registered. If the registration fails, the promise will reject.

## 3. Listening for life cycle events

Changes in the service worker's status trigger events in the service worker.

### 3.1 Add event listeners

Open __service-worker.js__ in your text editor.

Replace TODO 3.1 with the following code:

#### service-worker.js

```
self.addEventListener('install', function(event) {
  console.log('Service worker installing...');
});

self.addEventListener('activate', function(event) {
  console.log('Service worker activating...');
});
```

Save the file. Close __app/test/test-registered.html__ page if you have not already. Manually unregister the service worker and refresh the page to install and activate the updated service worker. The console log should indicate that the new service worker was registered, installed, and activated.

Note: All pages associated with the service worker must be closed before an updated service worker can take over.

Note: The registration log may appear out of order with the other logs (installation and activation). The service worker runs concurrently with the page, so we can't guarantee the order of the logs (the registration log comes from the page, while the installation and activation logs come from the service worker). Installation, activation, and other service worker events occur in a defined order inside the service worker, however, and should always appear in the expected order.

#### Explanation

The service worker emits an `install` event at the end of registration. In this case we log a message, but this is a good place for caching static assets.

When a service worker is registered, the browser detects if the service worker is new (either because it is different from the previously installed service worker or because there is no registered service worker for this site). If the service worker is new (as it is in this case) then the browser installs it.

The service worker emits an `activate` event when it takes control of the page. We log a message here, but this event is often used to update caches.

Only one service worker can be active at a time for a given scope (see  [Exploring service worker scope](#explore-scope)), so a newly installed service worker isn't activated until the existing service worker is no longer in use. This is why all pages controlled by a service worker must be closed before a new service worker can take over. Since we unregistered the existing service worker, the new service worker was activated immediately.

Note: Simply refreshing the page is not sufficient to transfer control to a new service worker, because the new page will be requested before the the current page is unloaded, and there won't be a time when the old service worker is not in use.

Note: You can also manually activate a new service worker using some browsers' developer tools and programatically with skipWaiting().

### 3.2 Re-register the existing service worker

Reload the page. Notice how the events change.

Now close and reopen the page (remember to close all pages associated with the service worker). Observe the logged events.

#### Explanation

After initial installation and activation, re-registering an existing worker does not re-install or re-activate the service worker. Service workers also persist across browsing sessions.

### 3.3 Update the service worker

Change the service worker by replacing TODO 3.3 with an arbitrary comment.

Save the file and refresh the page. Notice that the new service worker installs but does not activate.

Navigate to __test-waiting.html__ (__app/test/test-waiting.html__) to confirm that the new service worker is installed but not activated. The test should be passing (blue).

Close all pages associated with the service worker (including the __app/test/test-waiting.html__ page). Reopen the __app/__ page. The console log should indicate that the new service worker has now activated.

Note: If you are getting unexpected results, make sure your HTTP cache is disabled in developer tools.

#### Explanation

The browser detects a byte difference between the new and existing service worker file, so the new service worker is installed. Since only one service worker can be active at a time (for a given scope), even though the new service worker is installed, it isn't activated until the existing service worker is no longer in use. By closing all pages under the old service worker's control, we are able to activate the new service worker.

#### For more information

*  [Service worker lifecycle](/web/fundamentals/instant-and-offline/service-worker/lifecycle)

## 4. Intercept network requests

Service Workers can act as a proxy between your web app and the network.

Replace TODO 4 in service-worker.js with:

#### service-worker.js

```
self.addEventListener('fetch', function(event) {
  console.log('Fetching:', event.request.url);
});
```

Manually unregister the service worker and refresh the page to install and activate the updated service worker.

Check the console and observe that no fetch events were logged. Refresh the page and check the console again. You should see fetch events this time for the page and its assets (like CSS).

Click the links to __Other page__, __Another page__, and __Back__.

You'll see fetch events in the console for each of the pages and their assets. Do all the logs make sense?

Note: If you visit a page and do not have the HTTP cache disabled, CSS and JavaScript assets may be cached locally. If this occurs you will not see fetch events for these resources.

#### Explanation

The service worker receives a fetch event for every HTTP request made by the browser. The  [fetch event](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent) object contains the request. Listening for fetch events in the service worker is similar to listening to click events in the DOM. In our code, when a fetch event occurs, we log the requested URL to the console (in practice we could also create and return our own custom response with arbitrary resources).

Why didn't any fetch events log on the first refresh? By default, fetch events from a page won't go through a service worker unless the page request itself went through a service worker. This ensures consistency in your site; if a page loads without the service worker, so do its subresources.

#### For more information

*  [Fetch Event - MDN](https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent)
*  [Using Fetch - MDN](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)
*  [Introduction to Fetch - Google Developer](/web/updates/2015/03/introduction-to-fetch)

#### Solution code

To get a copy of the working code, navigate to the __04-intercepting-network-requests__ folder.

## 5. Optional: Exploring service worker scope

Service workers have scope. The scope of the service worker determines from which paths the service worker intercepts requests.

### 5.1 Find the scope

Update the registration code in __index.html__ with:

#### index.html

```
if (!('serviceWorker' in navigator)) {
  console.log('Service worker not supported');
  return;
}
navigator.serviceWorker.register('service-worker.js')
.then(function(registration) {
  console.log('Registered at scope:', registration.scope);
})
.catch(function(error) {
  console.log('Registration failed:', error);
});
```

Refresh the browser. Notice that the console shows the scope of the service worker (for example __http://localhost:8080/service-worker-lab/app/__).

#### Explanation

The promise returned by `register()` resolves to the  [registration object](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration), which contains the service worker's scope.

The default scope is the path to the service worker file, and extends to all lower directories. So a service worker in the root directory of an app controls requests from all files in the app.

### 5.2 Move the service worker

Move __service-worker.js__ into the __app/below__ directory and update the service worker URL in the registration code. Unregister the service worker and refresh the page.

The console shows that the scope of the service worker is now __localhost:8080/service-worker-lab/app/below/__.

Navigate to __test-scoped.html__ (__app/test/test-scoped.html__) to confirm that that service worker is registered in __app/below/__. If you've done everything correctly, you shouldn't see any red errors. Close the test page when you are done with it.

Back on the main page, click __Other page__,  __Another page__ and  __Back__. Which fetch requests are being logged? Which aren't?

#### Explanation

The service worker's default scope is the path to the service worker file. Since the service worker file is now in __app/below/__, that is its scope. The console is now only logging fetch events for __another.html__, __another.css__, and __another.js__, because these are the only resources within the service worker's scope (__app/below/__).

### 5.3 Set an arbitrary scope

Move the service worker back out into the project root directory (__app__) and update the service worker URL in the registration code.

Use the  [reference on MDN](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register) to set the scope of the service worker to the __app/below/__ directory using the optional parameter in `register()`. Unregister the service worker and refresh the page. Click __Other page__, __Another page__ and __Back__.

Again the console shows that the scope of the service worker is now __localhost:8080/service-worker-lab/app/below__, and logs fetch events only for __another.html__, __another.css__, and __another.js__.

Navigate to __test-scoped.html__ again to confirm that the service worker is registered in __app/below/__.

#### Explanation

It is possible to set an arbitrary scope by passing in an additional parameter when registering, for example:

#### index.html

```
navigator.serviceWorker.register('/service-worker.js', {
  scope: '/kitten/'
});
```

In the above example the scope of the service worker is set to __/kitten/__. The service worker intercepts requests from pages in __/kitten/__ and __/kitten/lower/__ but not from pages like __/kitten__ or __/__.

Note:  You cannot set an arbitrary scope that is above the service worker's actual location.

#### For more information

*  [Service worker registration object](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration)
*  [The register() method](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register)
*  [Service worker scope](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/scope)

#### Solution code

To get a copy of the working code, navigate to the __solution__ folder.

## Congratulations!

You now have a simple service worker up and running.


text:
function checkForm(form){

	var err = null;

	var inputList = $(form).find('input[checkplz]');
	if(inputList.length>0){

		inputList.each(function(){
			var checks = $(this).attr('checkplz').split(" ");
			var self = $(this);
			var globalResult = false;

			do {
				check = checks.shift();
				var result = globalResult = CheckContent(check, self);
				console.log('globalResult:', globalResult);
			}
			while (result && checks.length>0);

			console.log('globalResult:', globalResult);
			return globalResult;
		});

	}else{
		return true;
	}
}

function CheckContent(type, self){
	$('.errorMessage').hide();
	console.log('type:', type);
	if(type){
		switch(type) {
			case 'empty':
					if(!self.val()){
						var codeError = '#'+self.attr('name')+"_"+type;
						return showError(codeError);
					}
				return true;
				break;
			default:
				var result = true;
				$.ajax({
				   url : 'check/'+type+'/'+self.val(),
				   success : function(code_html, statut){
					   if(code_html!='1'){
						   var codeError = '#'+self.attr('name')+"_"+type;
						   result = showError(codeError);
						   console.log('result!!!!:', result);
					   }
				   },

				   error : function(resultat, statut, erreur){
					   var codeError = '#'+self.attr('name')+"_"+type;
						result = showError(codeError);
				   }

				});
console.log('result:', result);
				return result;
		}
	}

}

function showError(codeError){
	if($(codeError).length>0){
		$(codeError).show();
	}else{
		alert('ERR:'+codeError+" MISSING");
	}
	return false;

}


text:
<?php

return array(
    'xml_path' => WP_CONTENT_DIR . '/uploads/govright/sitemap',
    'apps' => array(
        'legislationlab' => 'Legislation Lab',
        'revtracker' => 'Revision Tracker',
    ),
    'gxmls_dep' => 'google-sitemap-generator/sitemap.php',
    'corpus_api_url' => 'http://corpus.govright.org/api'
);


text:
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using EventStore.BufferManagement;
using EventStore.Common.Log;
using EventStore.Common.Utils;
using EventStore.Core.Services.Transport.Tcp;
using EventStore.TestClient.Commands;
using EventStore.TestClient.Commands.DvuBasic;
using EventStore.Transport.Tcp;
using EventStore.Transport.Tcp.Formatting;
using EventStore.Transport.Tcp.Framing;
using Connection = EventStore.Transport.Tcp.TcpTypedConnection<byte[]>;

namespace EventStore.TestClient
{
    public class Client
    {
        private static readonly ILogger Log = LogManager.GetLoggerFor<Client>();

        public readonly bool InteractiveMode;

        public readonly ClientOptions Options;
        public readonly IPEndPoint TcpEndpoint;
        public readonly IPEndPoint HttpEndpoint;

        private readonly BufferManager _bufferManager = new BufferManager(TcpConfiguration.BufferChunksCount, TcpConfiguration.SocketBufferSize);
        private readonly TcpClientConnector _connector = new TcpClientConnector();

        private readonly CommandsProcessor _commands = new CommandsProcessor(Log);

        public Client(ClientOptions options)
        {
            Options = options;

            TcpEndpoint = new IPEndPoint(options.Ip, options.TcpPort);
            HttpEndpoint = new IPEndPoint(options.Ip, options.HttpPort);

            InteractiveMode = options.Command.IsEmpty();

            RegisterProcessors();
        }

        private void RegisterProcessors()
        {
            _commands.Register(new UsageProcessor(_commands), usageProcessor: true);
            _commands.Register(new ExitProcessor());

            _commands.Register(new PingProcessor());
            _commands.Register(new PingFloodProcessor());
            _commands.Register(new PingFloodWaitingProcessor());

            _commands.Register(new PingFloodHttpProcessor());

            _commands.Register(new WriteProcessor());
            _commands.Register(new WriteJsonProcessor());
            _commands.Register(new WriteFloodProcessor());
            _commands.Register(new WriteFloodClientApiProcessor());
            _commands.Register(new WriteFloodWaitingProcessor());

            _commands.Register(new MultiWriteProcessor());
            _commands.Register(new MultiWriteFloodWaitingProcessor());

            _commands.Register(new TransactionWriteProcessor());

            _commands.Register(new WriteHttpProcessor());
            _commands.Register(new WriteFloodHttpProcessor());
            _commands.Register(new WriteFloodWaitingHttpProcessor());

            _commands.Register(new DeleteProcessor());

            _commands.Register(new ReadAllProcessor());
            _commands.Register(new ReadProcessor());
            _commands.Register(new ReadFloodProcessor());

            _commands.Register(new ReadHttpProcessor());
            _commands.Register(new ReadFloodHttpProcessor());

            _commands.Register(new WriteLongTermProcessor());
            _commands.Register(new WriteLongTermHttpProcessor());
            _commands.Register(new PingHttpLongTermProcessor());

            _commands.Register(new DvuBasicProcessor());
            _commands.Register(new RunTestScenariosProcessor());

            _commands.Register(new SubscribeToStreamProcessor());

            _commands.Register(new ScavengeProcessor());

            _commands.Register(new TcpSanitazationCheckProcessor());

            _commands.Register(new SubscriptionStressTestProcessor());
        }

        public int Run()
        {
            if (!InteractiveMode)
                return Execute(Options.Command.ToArray());

            new Thread(() =>
            {
                Thread.Sleep(100);
                Console.Write(">>> ");

                string line;
                while ((line = Console.ReadLine()) != null)
                {
                    try
                    {
                        if (string.IsNullOrWhiteSpace(line))
                            continue;

                        try
                        {
                            var args = ParseCommandLine(line);
                            Execute(args);
                        }
                        catch (Exception exc)
                        {
                            Log.ErrorException(exc, "Error during executing command.");
                        }
                    }
                    finally
                    {
                        Thread.Sleep(100);
                        Console.Write(">>> ");
                    }
                }
            }) { IsBackground = true, Name = "Client Main Loop Thread" }.Start();
            return 0;
        }

        private static string[] ParseCommandLine(string line)
        {
            return line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        private int Execute(string[] args)
        {
            Log.Info("Processing command: {0}.", string.Join(" ", args));

            var context = new CommandProcessorContext(this, Log, new ManualResetEventSlim(true));

            int exitCode;
            if (_commands.TryProcess(context, args, out exitCode))
            {
                Log.Info("Command exited with code {0}.", exitCode);
                return exitCode;
            }

            return exitCode;
        }

        public Connection CreateTcpConnection(CommandProcessorContext context,
                                              Action<Connection, TcpPackage> handlePackage,
                                              Action<Connection> connectionEstablished = null,
                                              Action<Connection, SocketError> connectionClosed = null,
                                              bool failContextOnError = true,
                                              IPEndPoint tcpEndPoint = null)
        {
            var connectionCreatedEvent = new ManualResetEventSlim(false);
            Connection typedConnection = null;

            var connection = _connector.ConnectTo(
                Guid.NewGuid(),
                tcpEndPoint ?? TcpEndpoint,
                TcpConnectionManager.ConnectionTimeout,
                conn =>
                {
                    // we execute callback on ThreadPool because on FreeBSD it can be called synchronously
                    // causing deadlock
                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        if (!InteractiveMode)
                            Log.Info("TcpTypedConnection: connected to [{0}, L{1}, {2:B}].", conn.RemoteEndPoint, conn.LocalEndPoint, conn.ConnectionId);
                        if (connectionEstablished != null)
                        {
                            if (!connectionCreatedEvent.Wait(10000))
                                throw new Exception("TcpTypedConnection: creation took too long!");
                            connectionEstablished(typedConnection);
                        }
                    });
                },
                (conn, error) =>
                {
                    var message = string.Format("TcpTypedConnection: connection to [{0}, L{1}, {2:B}] failed. Error: {3}.",
                                                conn.RemoteEndPoint, conn.LocalEndPoint, conn.ConnectionId, error);
                    Log.Error(message);

                    if (connectionClosed != null)
                        connectionClosed(null, error);

                    if (failContextOnError)
                        context.Fail(reason: string.Format("Socket connection failed with error {0}.", error));
                },
                verbose: !InteractiveMode);

            typedConnection = new Connection(connection, new RawMessageFormatter(_bufferManager), new LengthPrefixMessageFramer());
            typedConnection.ConnectionClosed +=
                (conn, error) =>
                {
                    if (!InteractiveMode || error != SocketError.Success)
                    {
                        Log.Info("TcpTypedConnection: connection [{0}, L{1}] was closed {2}",
                                 conn.RemoteEndPoint, conn.LocalEndPoint,
                                 error == SocketError.Success ? "cleanly." : "with error: " + error + ".");
                    }

                    if (connectionClosed != null)
                        connectionClosed(conn, error);
                    else
                        Log.Info("connectionClosed callback was null");
                };
            connectionCreatedEvent.Set();

            typedConnection.ReceiveAsync(
                (conn, pkg) =>
                {
                    var package = new TcpPackage();
                    bool validPackage = false;
                    try
                    {
                        package = TcpPackage.FromArraySegment(new ArraySegment<byte>(pkg));
                        validPackage = true;

                        if (package.Command == TcpCommand.HeartbeatRequestCommand)
                        {
                            var resp = new TcpPackage(TcpCommand.HeartbeatResponseCommand, Guid.NewGuid(), null);
                            conn.EnqueueSend(resp.AsByteArray());
                            return;
                        }

                        handlePackage(conn, package);
                    }
                    catch (Exception ex)
                    {
                        Log.InfoException(ex,
                                          "TcpTypedConnection: [{0}, L{1}] ERROR for {2}. Connection will be closed.",
                                          conn.RemoteEndPoint, conn.LocalEndPoint,
                                          validPackage ? package.Command as object : "<invalid package>");
                        conn.Close(ex.Message);

                        if (failContextOnError)
                            context.Fail(ex);
                    }
                });

            return typedConnection;
        }
    }
}


text:
import React from 'react'
import {CSSTransitionGroup} from 'react-transition-group'
import {
  BrowserRouter as Router,
  Route,
  Link,
  Redirect
} from 'react-router-dom'

/* you'll need this CSS somewhere
 .fade-enter {
 opacity: 0;
 z-index: 1;
 }

 .fade-enter.fade-enter-active {
 opacity: 1;
 transition: opacity 250ms ease-in;
 }
 */

const AnimationExample = () =>

    <Route exact path='/' render={() =>
      <Redirect to='/10/90/50'/>
    }/>

      <NavLink to='/10/90/50'>Red</NavLink>
      <NavLink to='/120/100/40'>Green</NavLink>
      <NavLink to='/200/100/40'>Blue</NavLink>
      <NavLink to='/310/100/50'>Pink</NavLink>

      <CSSTransitionGroup
        transitionName='fade'
        transitionEnterTimeout={300}
        transitionLeaveTimeout={300}
      >
        {/* no different than other usage of
         CSSTransitionGroup, just make
         sure to pass `location` to `Route`
         so it can match the old location
         as it animates out
         */}
        <Route
          path='/:h/:s/:l'
          component={HSL}
        />
      </CSSTransitionGroup>

const NavLink = props =>

    <Link {...props} style={{color: 'inherit'}}/>

const HSL = ({match: {params}}) =>
  <div style={{
    ...styles.fill,
    ...styles.hsl,
    background: `hsl(${params.h}, ${params.s}%, ${params.l}%)`
  }}>hsl({params.h}, {params.s}%, {params.l}%)

const styles = {}

styles.fill = {
  position: 'absolute',
  left: 0,
  right: 0,
  top: 0,
  bottom: 0
}

styles.content = {
  ...styles.fill,
  top: '40px',
  textAlign: 'center'
}

styles.nav = {
  padding: 0,
  margin: 0,
  position: 'absolute',
  top: 0,
  height: '40px',
  width: '100%',
  display: 'flex'
}

styles.navItem = {
  textAlign: 'center',
  flex: 1,
  listStyleType: 'none',
  padding: '10px'
}

styles.hsl  = {
  ...styles.fill,
  color: 'white',
  paddingTop: '20px',
  fontSize: '30px'
}

export default AnimationExample


text:
package sacloud

import "time"

// Instance インスタンス
type Instance struct {
	*EServerInstanceStatus            // ステータス
	Server                 Resource   `json:",omitempty"` // サーバー
	StatusChangedAt        *time.Time `json:",omitempty"` // ステータス変更日時
	MigrationProgress      string     `json:",omitempty"` // コピージョブ進捗状態
	MigrationSchedule      string     `json:",omitempty"` // コピージョブスケジュール
	IsMigrating            bool       `json:",omitempty"` // コピージョブ実施中フラグ
	MigrationAllowed       string     `json:",omitempty"` // コピージョブ許可
	ModifiedAt             *time.Time `json:",omitempty"` // 変更日時
	CDROM                  *CDROM     `json:",omitempty"` // ISOイメージ
	CDROMStorage           *Storage   `json:",omitempty"` // ISOイメージストレージ

	Host struct { // Host
		Name          string `json:",omitempty"` // ホスト名
		InfoURL       string `json:",omitempty"` // インフォURL
		Class         string `json:",omitempty"` // クラス
		Version       int    `json:",omitempty"` // バージョン
		SystemVersion string `json:",omitempty"` // システムバージョン
	} `json:",omitempty"`
}

// HasInfoURL Host.InfoURLに値があるか
func (i *Instance) HasInfoURL() bool {
	return i != nil && i.Host.InfoURL != ""
}

// MaintenanceScheduled メンテナンス予定の有無
func (i *Instance) MaintenanceScheduled() bool {
	return i.HasInfoURL()
}


text:
	<title></title>
	<meta name="keywords" content="dshu.me" />

    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	   <link href="/css/main.css?v=3" rel="stylesheet" type="text/css" />

    <link rel="alternate" type="application/atom+xml" href="/atom.xml" title="Atom feed">

	<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=3"/>

            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                Toggle navigation

            </button>
            dshu's website

                         Blog

                         GitHub

Blog

    2019来了


text:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>dev.failsafe</groupId>
  <artifactId>failsafe-examples</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Failsafe Examples</name>

  <properties>
    <failsafe.version>3.2.1</failsafe.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>failsafe</artifactId>
      <version>${failsafe.version}</version>
    </dependency>

    <!-- Examples dependencies -->
    <dependency>
      <groupId>io.reactivex</groupId>
      <artifactId>rxjava</artifactId>
      <version>1.0.12</version>
    </dependency>
    <dependency>
      <groupId>io.netty</groupId>
      <artifactId>netty-all</artifactId>
      <version>4.1.51.Final</version>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-core</artifactId>
      <version>3.9.8</version>
    </dependency>

    <!-- Test Dependencies -->
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.9.10</version>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>4.2.0</version>
    </dependency>
  </dependencies>

</project>


text:
package org.apache.camel.component.resilience4j;

import org.apache.camel.RuntimeCamelException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Test;

import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ResilienceRouteFallbackViaNetworkTest extends CamelTestSupport {

    @Override
    public boolean isUseRouteBuilder() {
        return false;
    }

    @Test
    public void testResilience() throws Exception {
        context.addRoutes(createRouteBuilder());

        RuntimeCamelException exception = assertThrows(RuntimeCamelException.class, () -> context.start());
        assertIsInstanceOf(UnsupportedOperationException.class, exception.getCause());
        assertEquals("camel-resilience4j does not support onFallbackViaNetwork",
                exception.getCause().getMessage());
    }

    @Override
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start").to("log:start").circuitBreaker().throwException(new IllegalArgumentException("Forced"))
                        .onFallbackViaNetwork().transform()
                        .constant("Fallback message").end().to("log:result").to("mock:result");
            }
        };
    }

}


text:
package com.cerner.beadledom.swagger;

public class ImplementationResource implements InterfaceResource {
}


text:
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.Splash" parent="AppTheme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowAnimationStyle">@style/AppTheme.Animations.Splash</item>
    </style>

    <style name="AppTheme.Animations.Splash" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/splash_appear</item>
        <item name="android:windowExitAnimation">@anim/splash_disappear</item>
    </style>

    <style name="textAppearanceListItemTitle" parent="@style/TextAppearance.AppCompat.Large">
        <item name="android:textSize">20sp</item>
        <item name="android:textStyle">bold</item>
    </style>

    <style name="textAppearanceListItemText" parent="@style/TextAppearance.AppCompat.Medium">
        <item name="android:textSize">16sp</item>
        <item name="android:textStyle">normal</item>
    </style>

    <style name="textAppearanceListItemSmall" parent="@style/TextAppearance.AppCompat.Small">
        <item name="android:textSize">12sp</item>
        <item name="android:textStyle">normal</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

</resources>


text:
layout: post
title:  "Fungineering Podcast Episode 3: Why does Blake think AI is going to destroy the universe?"
date:   2017-08-23 18:46:38 -0700
categories: podcast
---
[![Fungineering Podcast Episode 3: Why does Blake think AI is going to destroy the universe?](/assets/images/fungineering brand assets_youtube thumbnail.png)](https://www.youtube.com/watch?v=s0jD4IMaHHs)


text:
body {
    background-color: red;
}

.body:after {
    content: url('assets/image2.jpg');
}

.image {
    width: auto;
    max-height: 120px;
    background: #FFFFFF url('assets/css-image.jpg') center center no-repeat;
}

.image-quotes {
    width: auto;
    max-height: 120px;
    background: #FFFFFF url("assets/css-image-quotes.jpg") center center no-repeat;
}

.large-image {
    width: 100%;
    background-image: url(assets/css-image-large.jpg);
    background-position: top center;
    background-size: contain;
}

.external-image {
    background-image: url('//www.external.com/external-image1.jpg');
    background-image: url('http://www.external.com/external-image2.jpg');
    background-image: url('https://www.external.com/external-image3.jpg');
}

@font-face {
    src: local('Lato-Hairline'), url('https://fonts.gstatic.com/s/lato/v11/zJY4gsxBiSo5L7tNutxFNg.ttf');
}

.data-url-image {
    background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwgAADsIBFShKgAAAABp0RVh0U29mdHdhcmUAUGFpbnQuTkVUIHYzLjUuMTAw9HKhAAAAmElEQVQoU33PQapBURjA8UtkwJuaWYGSgfQWYBMvczPmTCzAAGVuaA228BZhRCkDGSmE31FucuRfvzq3vr5zT/JSjSU7DsypEPXDkDVn2hSIytJhw4kWGaLCxgHh2gt/RBuLzNhz5caWPjnSqqw4EraFfwznf8qklWjwy4IRTerkiQoPGtPl40OehcEJvcfXl8LglLfBJLkDcMgbgHlHhK8AAAAASUVORK5CYII=);
}

@media only screen and (min-device-pixel-ratio: 2) and (min-resolution: 2dppx) {
    .large-image {
        background-image: url('assets/image1.jpg');
    }
}

@font-face {
    font-family: icons;
    src: url(/assets/fonts/icons.eot);
    src: url(/assets/fonts/icons.eot?#iefix) format(embedded-opentype),
         url(/assets/fonts/icons.woff) format(woff),
         url(/assets/fonts/icons.woff2) format(woff2),
         url(/assets/fonts/icons.ttf) format(truetype);
    font-weight: 400;
    font-style: normal;
}


text:
"""Generate a sitemap."""

from __future__ import print_function, absolute_import, unicode_literals
import io
import datetime
import dateutil.tz
import os
import sys
import robotstxt
try:
    from urlparse import urljoin, urlparse
except ImportError:
    from urllib.parse import urljoin, urlparse  # NOQA

from nikola.plugin_categories import LateTask
from nikola.utils import apply_filters, config_changed, encodelink, get_asset_path

urlset_header = """<?xml version="1.0" encoding="UTF-8"?>
<urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
                        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
"""

loc_format = """ <url>
  <loc>{0}</loc>
  <lastmod>{1}</lastmod>{2}
 </url>
"""

urlset_footer = "</urlset>"

sitemapindex_header = """<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
                        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
"""

sitemap_format = """ <sitemap>
  <loc>{0}</loc>
  <lastmod>{1}</lastmod>
 </sitemap>
"""

alternates_format = """\n  <xhtml:link rel="alternate" hreflang="{0}" href="{1}" />"""

sitemapindex_footer = "</sitemapindex>"

def get_base_path(base):
    """Return the path of a base URL if it contains one.

    >>> get_base_path('http://some.site') == '/'
    True
    >>> get_base_path('http://some.site/') == '/'
    True
    >>> get_base_path('http://some.site/some/sub-path') == '/some/sub-path/'
    True
    >>> get_base_path('http://some.site/some/sub-path/') == '/some/sub-path/'
    True
    """
    # first parse the base_url for some path
    base_parsed = urlparse(base)

    if not base_parsed.path:
        sub_path = ''
    else:
        sub_path = base_parsed.path
    if sub_path.endswith('/'):
        return sub_path
    else:
        return sub_path + '/'

class Sitemap(LateTask):
    """Generate a sitemap."""

    name = "sitemap"

    def gen_tasks(self):
        """Generate a sitemap."""
        kw = {
            "base_url": self.site.config["BASE_URL"],
            "site_url": self.site.config["SITE_URL"],
            "output_folder": self.site.config["OUTPUT_FOLDER"],
            "files_folders": self.site.config['FILES_FOLDERS'],
            "strip_indexes": self.site.config["STRIP_INDEXES"],
            "index_file": self.site.config["INDEX_FILE"],
            "sitemap_include_fileless_dirs": self.site.config["SITEMAP_INCLUDE_FILELESS_DIRS"],
            "mapped_extensions": self.site.config.get('MAPPED_EXTENSIONS', ['.atom', '.html', '.htm', '.php', '.xml', '.rss']),
            "robots_exclusions": self.site.config["ROBOTS_EXCLUSIONS"],
            "filters": self.site.config["FILTERS"],
            "translations": self.site.config["TRANSLATIONS"],
            "tzinfo": self.site.config['__tzinfo__'],
            "sitemap_plugin_revision": 1,
        }

        output = kw['output_folder']
        base_url = kw['base_url']
        mapped_exts = kw['mapped_extensions']

        output_path = kw['output_folder']
        sitemapindex_path = os.path.join(output_path, "sitemapindex.xml")
        sitemap_path = os.path.join(output_path, "sitemap.xml")
        base_path = get_base_path(kw['base_url'])
        sitemapindex = {}
        urlset = {}

        def scan_locs(robots_rules):
            """Scan site locations."""
            for root, dirs, files in os.walk(output, followlinks=True):
                if not dirs and not files and not kw['sitemap_include_fileless_dirs']:
                    continue  # Totally empty, not on sitemap
                path = os.path.relpath(root, output)
                # ignore the current directory.
                if path == '.':
                    path = ''
                else:
                    path = path.replace(os.sep, '/') + '/'
                lastmod = self.get_lastmod(root)
                loc = urljoin(base_url, base_path + path)
                if kw['index_file'] in files and kw['strip_indexes']:  # ignore folders when not stripping urls
                    post = self.site.post_per_file.get(path + kw['index_file'])
                    if post and (post.is_draft or post.is_private or post.publish_later):
                        continue
                    alternates = []
                    if post:
                        for lang in kw['translations']:
                            alt_url = post.permalink(lang=lang, absolute=True)
                            if encodelink(loc) == alt_url:
                                continue
                            alternates.append(alternates_format.format(lang, alt_url))
                    urlset[loc] = loc_format.format(encodelink(loc), lastmod, ''.join(alternates))
                for fname in files:
                    if kw['strip_indexes'] and fname == kw['index_file']:
                        continue  # We already mapped the folder
                    if os.path.splitext(fname)[-1] in mapped_exts:
                        real_path = os.path.join(root, fname)
                        path = os.path.relpath(real_path, output)
                        if path.endswith(kw['index_file']) and kw['strip_indexes']:
                            # ignore index files when stripping urls
                            continue
                        if robots_rules:
                            abspath = '/' + path
                            if sys.version_info[0] == 2:
                                abspath = abspath.encode('utf-8')
                            if not robots_rules.can_fetch('*', abspath):
                                continue

                        # read in binary mode to make ancient files work
                        fh = open(real_path, 'rb')
                        filehead = fh.read(1024)
                        fh.close()

                        if path.endswith('.html') or path.endswith('.htm') or path.endswith('.php'):
                            """ ignores "html" files without doctype """
                            if b'<!doctype html' not in filehead.lower():
                                continue

                            """ ignores "html" files with noindex robot directives """
                            robots_directives = [b'<meta content=noindex name=robots',
                                                 b'<meta content=none name=robots',
                                                 b'<meta name=robots content=noindex',
                                                 b'<meta name=robots content=none']
                            lowquothead = filehead.lower().decode('utf-8', 'ignore').replace('"', '').encode('utf-8')
                            if any([robot_directive in lowquothead for robot_directive in robots_directives]):
                                continue

                        # put Atom and RSS in sitemapindex[] instead of in urlset[],
                        # sitemap_path is included after it is generated
                        if path.endswith('.xml') or path.endswith('.atom') or path.endswith('.rss'):
                            known_elm_roots = (b'<feed', b'<rss', b'<urlset')
                            if any([elm_root in filehead.lower() for elm_root in known_elm_roots]) and path != sitemap_path:
                                path = path.replace(os.sep, '/')
                                lastmod = self.get_lastmod(real_path)
                                loc = urljoin(base_url, base_path + path)
                                sitemapindex[loc] = sitemap_format.format(encodelink(loc), lastmod)
                                continue
                            else:
                                continue  # ignores all XML files except those presumed to be RSS
                        post = self.site.post_per_file.get(path)
                        if post and (post.is_draft or post.is_private or post.publish_later):
                            continue
                        path = path.replace(os.sep, '/')
                        lastmod = self.get_lastmod(real_path)
                        loc = urljoin(base_url, base_path + path)
                        alternates = []
                        if post:
                            for lang in kw['translations']:
                                alt_url = post.permalink(lang=lang, absolute=True)
                                if encodelink(loc) == alt_url:
                                    continue
                                alternates.append(alternates_format.format(lang, alt_url))
                        urlset[loc] = loc_format.format(encodelink(loc), lastmod, '\n'.join(alternates))

        def parse_robotstxt(path):
            fh = io.open(path, 'r', encoding='utf-8-sig')
            rules = fh.readlines()
            if sys.version_info[0] == 2:
                rules = [ line.encode('utf-8') for line in rules ]
            fh.close()
            return robotstxt.TestAgent(base_url, robotstxt.parse(rules))

        def parse_robots_exclusions(exclusions):
            """Parse rules to check fetchable."""
            rules = []
            for rule in exclusions:
                rules.append('Disallow: {0}'.format(rule))
            if len(rules):
                rules = ['User-Agent: *'] + rules
                if sys.version_info[0] == 2:
                    rules = [ line.encode('utf-8') for line in rules ]
                return robotstxt.TestAgent(base_url, robotstxt.parse(rules))
            return None

        def write_sitemap():
            """Write sitemap to file."""
            # Have to rescan, because files may have been added between
            # task dep scanning and task execution
            with io.open(sitemap_path, 'w+', encoding='utf8') as outf:
                outf.write(urlset_header)
                for k in sorted(urlset.keys()):
                    outf.write(urlset[k])
                outf.write(urlset_footer)
            sitemap_url = urljoin(base_url, base_path + "sitemap.xml")
            sitemapindex[sitemap_url] = sitemap_format.format(sitemap_url, self.get_lastmod(sitemap_path))

        def write_sitemapindex():
            """Write sitemap index."""
            with io.open(sitemapindex_path, 'w+', encoding='utf8') as outf:
                outf.write(sitemapindex_header)
                for k in sorted(sitemapindex.keys()):
                    outf.write(sitemapindex[k])
                outf.write(sitemapindex_footer)

        def scan_locs_task():
            """Yield a task to calculate the dependencies of the sitemap.

            Other tasks can depend on this output, instead of having
            to scan locations.
            """
            robotstxt = get_asset_path("robots.txt", [], files_folders=kw["files_folders"], output_dir=False)
            if robotstxt:
                robots_rules = parse_robotstxt(robotstxt)
            else:
                robots_rules = parse_robots_exclusions(kw['robots_exclusions'])
            scan_locs(robots_rules)

            # Generate a list of file dependencies for the actual generation
            # task, so rebuilds are triggered.  (Issue #1032)
            output = kw["output_folder"]
            file_dep = []

            for i in urlset.keys():
                p = os.path.join(output, urlparse(i).path.replace(base_path, '', 1))
                if not p.endswith('sitemap.xml') and not os.path.isdir(p):
                    file_dep.append(p)
                if os.path.isdir(p) and os.path.exists(os.path.join(p, 'index.html')):
                    file_dep.append(p + 'index.html')

            for i in sitemapindex.keys():
                p = os.path.join(output, urlparse(i).path.replace(base_path, '', 1))
                if not p.endswith('sitemap.xml') and not os.path.isdir(p):
                    file_dep.append(p)
                if os.path.isdir(p) and os.path.exists(os.path.join(p, 'index.html')):
                    file_dep.append(p + 'index.html')

            if robotstxt:
                file_dep.append(os.path.join(output, 'robots.txt'))

            return {'file_dep': file_dep}

        yield {
            "basename": "_scan_locs",
            "name": "sitemap",
            "actions": [(scan_locs_task)]
        }

        yield self.group_task()
        yield apply_filters({
            "basename": "sitemap",
            "name": sitemap_path,
            "targets": [sitemap_path],
            "actions": [(write_sitemap,)],
            "uptodate": [config_changed(kw, 'nikola.plugins.task.sitemap:write')],
            "clean": True,
            "task_dep": ["render_site"],
            "calc_dep": ["_scan_locs:sitemap"],
        }, kw['filters'])
        yield apply_filters({
            "basename": "sitemap",
            "name": sitemapindex_path,
            "targets": [sitemapindex_path],
            "actions": [(write_sitemapindex,)],
            "uptodate": [config_changed(kw, 'nikola.plugins.task.sitemap:write_index')],
            "clean": True,
            "file_dep": [sitemap_path]
        }, kw['filters'])

    def get_lastmod(self, p):
        """Get last modification date."""
        if self.site.invariant:
            return '2038-01-01'
        else:
            # RFC 3339 (web ISO 8601 profile) represented in UTC with Zulu
            # zone desgignator as recommeded for sitemaps. Second and
            # microsecond precision is stripped for compatibility.
            lastmod = datetime.datetime.utcfromtimestamp(os.stat(p).st_mtime).replace(tzinfo=dateutil.tz.gettz('UTC'), second=0, microsecond=0).isoformat().replace('+00:00', 'Z')
            return lastmod

if __name__ == '__main__':
    import doctest
    doctest.testmod()


text:
package structtag

import (
	"errors"
	"fmt"
	"reflect"
)

// ReflectFunc type used to create funcrions to parse struct and tags.
type ReflectFunc func(
	field *reflect.StructField,
	value *reflect.Value,
	tag string) (err error)

var (
	// ErrNotAPointer error when not a pointer.
	ErrNotAPointer = errors.New("Not a pointer")

	// ErrNotAStruct error when not a struct.
	ErrNotAStruct = errors.New("Not a struct")

	// ErrTypeNotSupported error when type not supported.
	ErrTypeNotSupported = errors.New("Type not supported")

	// ErrUndefinedTag error when Tag var is not defined.
	ErrUndefinedTag = errors.New("Undefined tag")

	// Tag set the main tag.
	Tag string

	// TagDefault set tag default.
	TagDefault string

	// TagHelper set tag usage.
	TagHelper string

	// TagDisabled used to not process an input.
	TagDisabled string

	// TagSeparator separe names on environment variables.
	TagSeparator string

	// Prefix is a string that would be placed at the beginning of the generated tags.
	Prefix string

	// ParseMap points to each of the supported types.
	ParseMap map[reflect.Kind]ReflectFunc

	// ParsePakagesTypeMap points to each of the suported pakage or user defined types
	// and it's priority over ParseMap.
	ParsePakagesTypeMap map[string]ReflectFunc

	// ParseNameMap point to name of each fueld and functions are defined by the user
	// it's priority over ParsePakagesTypeMap and ParseMap.
	ParseNameMap map[string]ReflectFunc
)

// Setup maps and variables.
func Setup() {
	TagDisabled = "-"
	TagSeparator = "_"

	ParseMap = make(map[reflect.Kind]ReflectFunc)
	ParsePakagesTypeMap = make(map[string]ReflectFunc)
	ParseNameMap = make(map[string]ReflectFunc)

	ParseMap[reflect.Struct] = ReflectStruct
	ParseMap[reflect.Array] = ReflectArray
	ParseMap[reflect.Slice] = ReflectArray
}

// Reset maps caling setup function.
func Reset() {
	Setup()
}

func Parse(s interface{}, superTag string) (err error) {
	if Tag == "" {
		err = ErrUndefinedTag
		return
	}

	st := reflect.TypeOf(s)
	if st.Kind() != reflect.Ptr {
		err = ErrNotAPointer
		return
	}

	refField := st.Elem()
	if refField.Kind() != reflect.Struct {
		err = ErrNotAStruct
		return
	}

	refValue := reflect.ValueOf(s).Elem()
	for i := 0; i < refField.NumField(); i++ {
		field := refField.Field(i)
		value := refValue.Field(i)
		kind := field.Type.Kind()

		if field.PkgPath != "" {
			continue
		}

		t := updateTag(&field, superTag)
		if t == "" {
			continue
		}

		f, ok := ParseNameMap[field.Type.Name()]
		if ok {
			err = f(&field, &value, t)
			if err != nil {
				return
			}
			continue
		}

		f, ok = ParsePakagesTypeMap[field.Type.String()]
		if ok {
			err = f(&field, &value, t)
			if err != nil {
				return
			}
			continue
		}

		f, ok = ParseMap[kind]
		if !ok {
			err = ErrTypeNotSupported
			return
		}

		err = f(&field, &value, t)
		if err != nil {
			return
		}
	}
	return
}

// SetBoolDefaults populates the boolean fields of 's' with cfgDefault values.
func SetBoolDefaults(s interface{}, superTag string) (err error) {
	if Tag == "" {
		err = ErrUndefinedTag
		return
	}

	st := reflect.TypeOf(s)
	if st.Kind() != reflect.Ptr {
		err = ErrNotAPointer
		return
	}

	refField := st.Elem()
	if refField.Kind() != reflect.Struct {
		err = ErrNotAStruct
		return
	}

	refValue := reflect.ValueOf(s).Elem()
	for i := 0; i < refField.NumField(); i++ {
		field := refField.Field(i)
		kind := field.Type.Kind()
		value := refValue.Field(i)

		if kind == reflect.Bool {
			if field.PkgPath != "" {
				continue
			}

			t := updateTag(&field, superTag)
			if t == "" {
				continue
			}

			defaultValue := field.Tag.Get(TagDefault)
			v := defaultValue == "true" || defaultValue == "t"
			value.SetBool(v)
		} else if kind == reflect.Struct {
			t := updateTag(&field, superTag)
			if t != "" {
				err := SetBoolDefaults(value.Addr().Interface(), "")
				if err != nil {
					return err
				}
			}
		}
	}
	return
}

func updateTag(field *reflect.StructField, superTag string) (ret string) {
	ret = field.Tag.Get(Tag)
	if ret == TagDisabled {
		ret = ""
		return
	}
	if ret == "" {
		ret = field.Name
	}
	if superTag != "" {
		ret = superTag + TagSeparator + ret
		return
	}
	if Prefix != "" {
		ret = Prefix + TagSeparator + ret
	}
	return
}

// ReflectStruct is called when the Parse encounters a sub-structure in the current structure and then calls Parser again to treat the fields of the sub-structure.
func ReflectStruct(field *reflect.StructField, value *reflect.Value, tag string) (err error) {
	err = Parse(value.Addr().Interface(), tag)
	return
}

// ReflectArray is called when the Parse encounters a sub-array in the current structure and then calls Parser again to treat the fields of the sub-array.
func ReflectArray(field *reflect.StructField, value *reflect.Value, tag string) (err error) {
	req := field.Tag.Get("cfgRequired")
	if req == "true" && value.Len() == 0 {
		err = fmt.Errorf("-%v is required", tag)
		return
	}
	switch value.Type().Elem().Kind() {
	case reflect.Struct, reflect.Slice, reflect.Array, reflect.Ptr, reflect.Interface:
		for i := 0; i < value.Len(); i++ {
			err = Parse(value.Index(i).Addr().Interface(), fmt.Sprintf("%s[%d]", tag, i))
			if err != nil {
				return
			}
		}
	}
	return
}


text:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.lessvoid.nifty.controls;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * This interface allows to take control over a single tab. This tab is supposed to be the part of a {@link TabGroup}.
 * For the most aspects it behaves like a simple panel.
 *
 * @author ractoc
 * @author Martin Karing <nitram@illarion.org>
 */
public interface Tab extends NiftyControl {
  /**
   * Get the caption that is currently set for this tab.
   *
   * @return the current caption of this tab
   */
  @Nonnull
  String getCaption();

  /**
   * Get the parent group of tabs this tab is a member of.
   *
   * @return the tab group this tab belongs to or {@code null} in case this tab is not yet assigned to a tab group
   */
  @Nullable
  TabGroup getParentGroup();

  /**
   * Check if this tab was added to a parent tab group.
   *
   * @return {@code true} if this tab is part of a group
   */
  boolean hasParent();

  /**
   * Check if this tab is the one that is currently visible in a tab group.
   *
   * @return {@code true} if this tab is the one currently displayed
   */
  boolean isVisible();

  /**
   * Set the caption of the tab. This is the text that will be displayed in the button that turns this tab visible.
   *
   * @param caption the text to display
   */
  void setCaption(@Nonnull String caption);
}


text:
<?php
// autogenerated file 30.09.2013 15:20
// $Id: $
// $Log: $
//
require_once 'EbatNs_FacetType.php';

/**
 * eBay sites (by the country in which each resides) on which a user is
 * registeredand on which items can be listed.
 *
 * @link http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/types/SiteCodeType.html
 *
 * @property string US
 * @property string Canada
 * @property string UK
 * @property string Australia
 * @property string Austria
 * @property string Belgium_French
 * @property string France
 * @property string Germany
 * @property string Italy
 * @property string Belgium_Dutch
 * @property string Netherlands
 * @property string Spain
 * @property string Switzerland
 * @property string Taiwan
 * @property string eBayMotors
 * @property string HongKong
 * @property string Singapore
 * @property string India
 * @property string China
 * @property string Ireland
 * @property string Malaysia
 * @property string Philippines
 * @property string Poland
 * @property string Sweden
 * @property string CustomCode
 * @property string CanadaFrench
 */
class SiteCodeType extends EbatNs_FacetType
{
	const CodeType_US = 'US';
	const CodeType_Canada = 'Canada';
	const CodeType_UK = 'UK';
	const CodeType_Australia = 'Australia';
	const CodeType_Austria = 'Austria';
	const CodeType_Belgium_French = 'Belgium_French';
	const CodeType_France = 'France';
	const CodeType_Germany = 'Germany';
	const CodeType_Italy = 'Italy';
	const CodeType_Belgium_Dutch = 'Belgium_Dutch';
	const CodeType_Netherlands = 'Netherlands';
	const CodeType_Spain = 'Spain';
	const CodeType_Switzerland = 'Switzerland';
	const CodeType_Taiwan = 'Taiwan';
	const CodeType_eBayMotors = 'eBayMotors';
	const CodeType_HongKong = 'HongKong';
	const CodeType_Singapore = 'Singapore';
	const CodeType_India = 'India';
	const CodeType_China = 'China';
	const CodeType_Ireland = 'Ireland';
	const CodeType_Malaysia = 'Malaysia';
	const CodeType_Philippines = 'Philippines';
	const CodeType_Poland = 'Poland';
	const CodeType_Sweden = 'Sweden';
	const CodeType_CustomCode = 'CustomCode';
	const CodeType_CanadaFrench = 'CanadaFrench';

	/**
	 * @return
	 */
	function __construct()
	{
		parent::__construct('SiteCodeType', 'urn:ebay:apis:eBLBaseComponents');

	}
}

$Facet_SiteCodeType = new SiteCodeType();

?>