/** * get请求 * * @param context * @param url * @param params * @param responseListener * @throws NetworkErrorException */ public static void request( final Context context, String url, Bundle params, ResponseListener responseListener) { mLogger = Logger.getLogger(); if (params != null) { if (url.contains("?")) { url = url + "&" + UrlUtils.encodeUrl(params); } else { url = url + "?" + UrlUtils.encodeUrl(params); } } Logger.getLogger().d("GET:" + url); HttpGet request = new HttpGet(url); HttpClient httpClient = getInstance(context); // 解决:HttpClient WARNING: Cookie rejected: Illegal domain attribute httpClient .getParams() .setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); String response = ""; try { response = httpClient.execute(request, new BasicResponseHandler()); if (!TextUtils.isEmpty(response)) { responseListener.onComplete(response); } else { responseListener.onComplete(""); } } catch (IOException e) { e.printStackTrace(); if (e instanceof ConnectTimeoutException) { mLogger.e("error ConnectTimeoutException."); } mLogger.e("error begin."); mLogger.e("error params:"); mLogger.e("error.url(GET):" + url); mLogger.e("error.getMessage:" + e.getMessage()); mLogger.e("error end."); responseListener.onFail(new MyCosIOException("request url IOException", e)); } catch (Exception e) { e.printStackTrace(); mLogger.e("error:" + e.getMessage()); responseListener.onFail(new MyCosOtherException("request url Exception", e)); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { final Channel channel = ctx.getChannel(); SocketMessage sm = (SocketMessage) e.getMessage(); if (sm.getKind() == Kind.REQUEST) { final Request request = Request.newBuilder().mergeFrom(sm.getBody()).build(); if (request.getOperate() == Operate.HeartBeat) { beHeartBeat.beHeartBeat(context, channel, request); } } else if (sm.getKind() == Kind.WEB_REUQEST) { final WebRequest request = WebRequest.newBuilder().mergeFrom(sm.getBody()).build(); if (request.getOperate() == WebOperate.ExecuteJob) { completionService.submit( new Callable<ChannelResponse>() { public ChannelResponse call() throws Exception { return new ChannelResponse(channel, beWebExecute.beWebExecute(context, request)); } }); } else if (request.getOperate() == WebOperate.CancelJob) { completionService.submit( new Callable<ChannelResponse>() { public ChannelResponse call() throws Exception { return new ChannelResponse(channel, beWebCancel.beWebCancel(context, request)); } }); } else if (request.getOperate() == WebOperate.UpdateJob) { completionService.submit( new Callable<ChannelResponse>() { public ChannelResponse call() throws Exception { return new ChannelResponse(channel, beUpdate.beWebUpdate(context, request)); } }); } else if (request.getOperate() == WebOperate.ExecuteDebug) { completionService.submit( new Callable<ChannelResponse>() { public ChannelResponse call() throws Exception { return new ChannelResponse(channel, beDebug.beWebExecute(context, request)); } }); } } else if (sm.getKind() == Kind.RESPONSE) { for (ResponseListener lis : new ArrayList<ResponseListener>(listeners)) { lis.onResponse(Response.newBuilder().mergeFrom(sm.getBody()).build()); } } else if (sm.getKind() == Kind.WEB_RESPONSE) { for (ResponseListener lis : new ArrayList<ResponseListener>(listeners)) { lis.onWebResponse(WebResponse.newBuilder().mergeFrom(sm.getBody()).build()); } } super.messageReceived(ctx, e); }
/** * post请求 * * @param context * @param url * @param params * @param responseListener */ public static void post( Context context, String url, ArrayList<NameValuePair> params, ResponseListener responseListener) { mLogger = Logger.getLogger(); try { mLogger.d("POST:" + url); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, CHARSET); HttpPost request = new HttpPost(url); request.setEntity(entity); HttpClient client = getInstance(context); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity resEntity = response.getEntity(); String ret = (resEntity == null) ? null : EntityUtils.toString(resEntity, CHARSET); responseListener.onComplete(ret); } else { responseListener.onComplete(""); } } catch (IOException e) { e.printStackTrace(); mLogger.e("error begin."); mLogger.e("error params:"); mLogger.e("error.url(POST):" + url); mLogger.e("error.getMessage:" + e.getMessage()); mLogger.e("error end."); responseListener.onFail(new MyCosIOException("request url IOException", e)); } catch (Exception e) { e.printStackTrace(); mLogger.e("error:" + e.getMessage()); responseListener.onFail(new MyCosOtherException("request url Exception", e)); } }
/** * post string * * @param context * @param url * @param responseListener */ public static void post( Context context, String url, String jsonString, ResponseListener responseListener) { mLogger = Logger.getLogger(); try { HttpPost request = new HttpPost(url); StringEntity entity = new StringEntity(jsonString); request.setEntity(entity); request.setHeader("Accept", "application/json"); request.setHeader("Content-type", "application/json"); mLogger.d("POST:" + url); mLogger.d("BODY:" + jsonString); HttpClient client = getInstance(context); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity resEntity = response.getEntity(); String ret = (resEntity == null) ? null : EntityUtils.toString(resEntity, CHARSET); responseListener.onComplete(ret); } else { responseListener.onComplete(""); } } catch (IOException e) { e.printStackTrace(); mLogger.e("error begin."); mLogger.e("error POST BODY:"); mLogger.e("error.url(POST):" + url); mLogger.e("error.getMessage:" + e.getMessage()); mLogger.e("error end."); responseListener.onFail(new MyCosIOException("request url IOException", e)); } catch (Exception e) { e.printStackTrace(); mLogger.e("error:" + e.getMessage()); responseListener.onFail(new MyCosOtherException("request url Exception", e)); } }
private void triggerResponseHandler(HttpResponse httpResponse) { // For more simplify interface to handle the response if (responseLiteListener != null) responseLiteListener.onFinished(httpResponse); if (responseListener == null) return; // Invalid request if (httpResponse.isCorrupt()) { responseListener.onCorrupt(); return; } int status = httpResponse.getStatusCode(); if (status >= 500) { responseListener.onInternalError(httpResponse); } else if (status >= 400) { responseListener.onFailed(httpResponse); } else if (status >= 300) { responseListener.onRedirect(httpResponse); } else if (status >= 200) { responseListener.onSuccess(httpResponse); } }
/** * Gets the images for multiple filters on a image. This can be used to quickly get thumbnail * images for filters. <br> * Whenever a new Bitmap is ready, the listener will be called with the bitmap. The order of the * calls to the listener will be the same as the filter order. * * @param bitmap the bitmap on which the filters will be applied * @param filters the filters which will be applied on the bitmap * @param listener the listener on which the results will be notified */ public static void getBitmapForMultipleFilters( final Bitmap bitmap, final List<GPUImageFilter> filters, final ResponseListener<Bitmap> listener) { if (filters.isEmpty()) { return; } GPUImageRenderer renderer = new GPUImageRenderer(filters.get(0)); renderer.setImageBitmap(bitmap, false); PixelBuffer buffer = new PixelBuffer(bitmap.getWidth(), bitmap.getHeight()); buffer.setRenderer(renderer); for (GPUImageFilter filter : filters) { renderer.setFilter(filter); listener.response(buffer.getBitmap()); filter.destroy(); } renderer.deleteImage(); buffer.destroy(); }
public boolean patternInitiationForWithMultiWithEmptyBodyAndQosAndSessionAndTransistionsAndBehaviourIdTest( String pattern, boolean callMultiVersion, boolean callEmptyVersion, String qosLevel, String sessionType, String[] transistions, int procedureId) throws Exception { LoggingBase.logMessage( "PatternTest(" + pattern + ", " + callMultiVersion + ", " + qosLevel + ", " + sessionType + ", " + procedureId + ")"); resetAssertions(); QoSLevel qos = ParseHelper.parseQoSLevel(qosLevel); SessionType session = ParseHelper.parseSessionType(sessionType); Identifier sessionName = PubSubTestCaseHelper.getSessionName(session); initConsumer(session, sessionName, qos); IPTestStub ipTest = ipTestConsumer.getStub(); ep = TransportInterceptor.instance().getEndPoint(ipTestConsumer.getConsumer().getURI()); MALMessageHeader expectedInitialHeader = new TestMessageHeader( ipTestConsumer.getConsumer().getURI(), HeaderTestProcedure.AUTHENTICATION_ID, ipTestConsumer.getConsumer().getURI(), new Time(System.currentTimeMillis()), qos, HeaderTestProcedure.PRIORITY, HeaderTestProcedure.DOMAIN, HeaderTestProcedure.NETWORK_ZONE, session, sessionName, InteractionType.PUBSUB, new UOctet(MALPubSubOperation._REGISTER_STAGE), null, // transaction id not checked here (see below) MALPrototypeHelper.MALPROTOTYPE_AREA_NUMBER, IPTestHelper.IPTEST_SERVICE_NUMBER, IPTestHelper.MONITOR_OP.getNumber(), MALPrototypeHelper.MALPROTOTYPE_AREA.getVersion(), Boolean.FALSE); MALMessageHeader expectedFinalHeader = new TestMessageHeader( ipTestConsumer.getConsumer().getURI(), TestServiceProvider.IP_TEST_AUTHENTICATION_ID, ipTestConsumer.getConsumer().getURI(), new Time(System.currentTimeMillis()), qos, HeaderTestProcedure.PRIORITY, HeaderTestProcedure.DOMAIN, HeaderTestProcedure.NETWORK_ZONE, session, sessionName, InteractionType.PUBSUB, new UOctet(MALPubSubOperation._REGISTER_STAGE), null, // transaction id not checked here (see below) MALPrototypeHelper.MALPROTOTYPE_AREA_NUMBER, IPTestHelper.IPTEST_SERVICE_NUMBER, IPTestHelper.MONITOR_OP.getNumber(), MALPrototypeHelper.MALPROTOTYPE_AREA.getVersion(), Boolean.FALSE); IPTestTransitionList transList = new IPTestTransitionList(); List initialFaultyTransList = new LinkedList(); List finalFaultyTransList = new LinkedList(); boolean seenGoodTransition = false; int expectedTransitionCount = 0; for (String trans : transistions) { IPTestTransitionType transition = IPTestTransitionTypeFromString(trans); if (trans.startsWith("_")) { if (seenGoodTransition) { finalFaultyTransList.add(transition); } else { ++expectedTransitionCount; initialFaultyTransList.add(transition); } // there should be no more transitions after a faulty one break; } else { if (initialFaultyTransList.isEmpty()) { seenGoodTransition = true; ++expectedTransitionCount; } } transList.add(new IPTestTransition(transition, null)); } IPTestDefinition testDef = new IPTestDefinition( String.valueOf(procedureId), ipTestConsumer.getConsumer().getURI(), HeaderTestProcedure.AUTHENTICATION_ID, qos, HeaderTestProcedure.PRIORITY, HeaderTestProcedure.DOMAIN, HeaderTestProcedure.NETWORK_ZONE, session, sessionName, transList, new Time(System.currentTimeMillis())); ResponseListener monitor = new ResponseListener("PatternTest", expectedTransitionCount); setupInitialFaultyTransitions(initialFaultyTransList); if ("SUBMIT".equalsIgnoreCase(pattern)) { expectedInitialHeader.setInteractionType(InteractionType.SUBMIT); expectedInitialHeader.setInteractionStage(MALSubmitOperation.SUBMIT_STAGE); if (callMultiVersion) { expectedInitialHeader.setOperation(IPTestHelper.SUBMITMULTI_OP.getNumber()); } else { expectedInitialHeader.setOperation(IPTestHelper.TESTSUBMIT_OP.getNumber()); } testSubmit(monitor, ipTest, callMultiVersion, testDef); } else if ("REQUEST".equalsIgnoreCase(pattern)) { expectedInitialHeader.setInteractionType(InteractionType.REQUEST); expectedInitialHeader.setInteractionStage(MALRequestOperation.REQUEST_STAGE); if (callMultiVersion) { expectedInitialHeader.setOperation(IPTestHelper.REQUESTMULTI_OP.getNumber()); } else { if (callEmptyVersion) { expectedInitialHeader.setOperation(IPTestHelper.TESTREQUESTEMPTYBODY_OP.getNumber()); } else { expectedInitialHeader.setOperation(IPTestHelper.REQUEST_OP.getNumber()); } } testRequest(monitor, ipTest, callMultiVersion, callEmptyVersion, testDef); } else if ("INVOKE".equalsIgnoreCase(pattern)) { expectedInitialHeader.setInteractionType(InteractionType.INVOKE); expectedInitialHeader.setInteractionStage(MALInvokeOperation.INVOKE_STAGE); if (callMultiVersion) { expectedInitialHeader.setOperation(IPTestHelper.INVOKEMULTI_OP.getNumber()); } else { if (callEmptyVersion) { expectedInitialHeader.setOperation(IPTestHelper.TESTINVOKEEMPTYBODY_OP.getNumber()); } else { expectedInitialHeader.setOperation(IPTestHelper.INVOKE_OP.getNumber()); } } testInvoke(monitor, ipTest, callMultiVersion, callEmptyVersion, testDef); } else if ("PROGRESS".equalsIgnoreCase(pattern)) { expectedInitialHeader.setInteractionType(InteractionType.PROGRESS); expectedInitialHeader.setInteractionStage(MALProgressOperation.PROGRESS_STAGE); if (callMultiVersion) { expectedInitialHeader.setOperation(IPTestHelper.PROGRESSMULTI_OP.getNumber()); } else { if (callEmptyVersion) { expectedInitialHeader.setOperation(IPTestHelper.TESTPROGRESSEMPTYBODY_OP.getNumber()); } else { expectedInitialHeader.setOperation(IPTestHelper.PROGRESS_OP.getNumber()); } } testProgress(monitor, ipTest, callMultiVersion, callEmptyVersion, testDef); } MALMessageHeader msgHeader = addInitialHeaderAssertions(expectedInitialHeader); expectedFinalHeader.setInteractionType(expectedInitialHeader.getInteractionType()); expectedFinalHeader.setOperation(expectedInitialHeader.getOperation()); expectedFinalHeader.setURIFrom(msgHeader.getURITo()); expectedFinalHeader.setTransactionId(msgHeader.getTransactionId()); sendInitialFaultyTransitions(initialFaultyTransList, expectedFinalHeader); boolean retVal = false; try { LoggingBase.logMessage("PatternTest.waiting for responses"); retVal = monitor.cond.waitFor(10000); } catch (InterruptedException ex) { // do nothing, we are expecting this } LoggingBase.logMessage("PatternTest.waiting(" + retVal + ")"); if (retVal) { sendFinalFaultyTransitions(finalFaultyTransList, expectedFinalHeader); if ("SUBMIT".equalsIgnoreCase(pattern)) { retVal = addSubmitReturnAssertions(monitor, procedureId, expectedFinalHeader); } else if ("REQUEST".equalsIgnoreCase(pattern)) { retVal = addRequestReturnAssertions(monitor, procedureId, expectedFinalHeader); } else if ("INVOKE".equalsIgnoreCase(pattern)) { retVal = addInvokeReturnAssertions(monitor, procedureId, expectedFinalHeader); } else if ("PROGRESS".equalsIgnoreCase(pattern)) { retVal = addProgressReturnAssertions(monitor, procedureId, expectedFinalHeader); } } LoggingBase.logMessage("PatternTest" + pattern + "(" + retVal + ")"); correctNumberOfTransistions = monitor.checkCorrectNumberOfReceivedMessages(); return retVal; }
@Override public void onSuccess(int statusCode, Header[] headers, String responseString) { responseListener.onSuccess(responseString); }
@Override public void onFailure( int statusCode, Header[] headers, String responseString, Throwable throwable) { responseListener.onFail(responseString); }