Пример #1
0
 /**
  * 404エラーレスポンスを生成する.
  *
  * @param requestId リクエストID
  * @return 404レスポンス
  */
 private String create404(String requestId) {
   JsonRpcResponse res = new JsonRpcResponse();
   res.setId(requestId);
   RpcFault f = new RpcFault();
   f.setFaultCode("404");
   f.setFaultString("method not found");
   f.setDetail("method not found");
   res.setError(f);
   return JSON.encode(res);
 }
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    Map<String, Object> httpHeaders = new Hashtable<String, Object>();
    List<RpcHeader> headers = new ArrayList<RpcHeader>();
    Pair<Endpoint, Long> r = preprocess(httpHeaders, headers);

    Endpoint endpoint = r.getFirst();
    long iid = r.getSecond();

    URL url = endpoint.getAddress().toURL();
    String key =
        url.getProtocol()
            + "://"
            + url.getAuthority()
            + url.getPath()
            + "##"
            + method.getName()
            + "("
            + JSON.encode(args)
            + ")";
    long s = System.currentTimeMillis();
    Object ret = cache.getIfPresent(key);
    if (ret != null) {
      postprocess(
          iid, System.currentTimeMillis() - s, new MimeHeaders(), new ArrayList<RpcHeader>(), null);
      return ret;
    } else {
      Object client = clientFactory.create(interfaceClass, endpoint.getAddress().toURL());
      RequestAttributes reqAttrs = (RequestAttributes) client;
      reqAttrs.setUserId(endpoint.getUserName());
      reqAttrs.setPassword(endpoint.getPassword());
      reqAttrs.addRequestMimeHeaders(httpHeaders);
      for (RpcHeader h : headers) {
        reqAttrs.addRequestRpcHeader(h.getNamespace(), h.getName(), h.getValue());
      }
      try {
        Object result = method.invoke(client, args);
        cache.put(key, result);
        return result;
      } finally {
        ResponseAttributes resAttrs = (ResponseAttributes) client;
        postprocess(
            iid,
            System.currentTimeMillis() - s,
            MimeHeadersUtil.fromStringObjectMap(resAttrs.getResponseMimeHeaders()),
            resAttrs.getResponseRpcHeaders(),
            resAttrs.getResponseRpcFault());
      }
    }
  }