org.apache.http.HttpResponse Scala Examples
The following examples show how to use org.apache.http.HttpResponse.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: DWSHttpClient.scala From Linkis with Apache License 2.0 | 5 votes |
class DWSHttpClient(clientConfig: DWSClientConfig, clientName: String) extends AbstractHttpClient(clientConfig, clientName) { override protected def createDiscovery(): Discovery = new DWSGatewayDiscovery override protected def prepareAction(requestAction: HttpAction): HttpAction = { requestAction match { case dwsAction: DWSHttpAction => dwsAction.setDWSVersion(clientConfig.getDWSVersion) case _ => } requestAction } override protected def httpResponseToResult(response: HttpResponse, requestAction: HttpAction, responseBody: String): Option[Result] = { var entity = response.getEntity val statusCode: Int = response.getStatusLine.getStatusCode val url: String = requestAction.getURL val contentType: String = entity.getContentType.getValue DWSHttpMessageFactory.getDWSHttpMessageResult(url).map { case DWSHttpMessageResultInfo(_, clazz) => clazz match { case c if ClassUtils.isAssignable(c, classOf[DWSResult]) => val dwsResult = clazz.getConstructor().newInstance().asInstanceOf[DWSResult] dwsResult.set(responseBody, statusCode, url, contentType) BeanUtils.populate(dwsResult, dwsResult.getData) return Some(dwsResult) case _ => } def transfer(value: Result, map: Map[String, Object]): Unit = { value match { case httpResult: HttpResult => httpResult.set(responseBody, statusCode, url, contentType) case _ => } val javaMap = mapAsJavaMap(map) BeanUtils.populate(value, javaMap) fillResultFields(javaMap, value) } deserializeResponseBody(response) match { case map: Map[String, Object] => val value = clazz.getConstructor().newInstance().asInstanceOf[Result] transfer(value, map) value case list: List[Map[String, Object]] => val results = list.map { map => val value = clazz.getConstructor().newInstance().asInstanceOf[Result] transfer(value, map) value }.toArray new ListResult(responseBody, results) } }.orElse(nonDWSResponseToResult(response, requestAction)) } protected def nonDWSResponseToResult(response: HttpResponse, requestAction: HttpAction): Option[Result] = None protected def fillResultFields(responseMap: util.Map[String, Object], value: Result): Unit = {} //TODO Consistent with workspace, plus expiration time(与workspace保持一致,加上过期时间) override protected def getFsByUser(user: String, path: FsPath): Fs = FSFactory.getFsByProxyUser(path, user) } object DWSHttpClient { val jacksonJson = new ObjectMapper().setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")) }
Example 2
Source File: DWSGatewayDiscovery.scala From Linkis with Apache License 2.0 | 5 votes |
class DWSGatewayDiscovery extends AbstractDiscovery { override protected def discovery(): Array[String] = { val serverUrl = if(getServerInstances.isEmpty) getServerUrl else Random.shuffle(getServerInstances.toList).head info("discovery to gateway " + serverUrl) getClient.execute(getHeartbeatAction(serverUrl)) match { case dws: DWSHeartbeatResult => dws.getGatewayList } } override protected def getHeartbeatAction(serverUrl: String): HeartbeatAction = new DWSHeartbeatAction(serverUrl) override def getHeartbeatResult(response: HttpResponse, requestAction: HeartbeatAction): HeartbeatResult = requestAction match { case h: DWSHeartbeatAction => new DWSHeartbeatResult(response, h.serverUrl) } }
Example 3
Source File: StaticAuthenticationStrategy.scala From Linkis with Apache License 2.0 | 5 votes |
package com.webank.wedatasphere.linkis.httpclient.dws.authentication import com.webank.wedatasphere.linkis.common.utils.ByteTimeUtils import com.webank.wedatasphere.linkis.httpclient.authentication.{AbstractAuthenticationStrategy, AuthenticationAction, AuthenticationResult} import com.webank.wedatasphere.linkis.httpclient.dws.exception.AuthenticationFailedException import com.webank.wedatasphere.linkis.httpclient.dws.request.DWSAuthenticationAction import com.webank.wedatasphere.linkis.httpclient.dws.response.DWSAuthenticationResult import com.webank.wedatasphere.linkis.httpclient.request.{Action, UserAction, UserPwdAction} import org.apache.commons.lang.StringUtils import org.apache.http.HttpResponse class StaticAuthenticationStrategy(override protected val sessionMaxAliveTime: Long) extends AbstractAuthenticationStrategy { def this() = this(ByteTimeUtils.timeStringAsMs("2h")) override protected def getAuthenticationAction(requestAction: Action, serverUrl: String): AuthenticationAction = { val action = new DWSAuthenticationAction(serverUrl) def pwd: String = if(StringUtils.isNotBlank(getClientConfig.getAuthTokenValue)) getClientConfig.getAuthTokenValue else throw new AuthenticationFailedException("the value of authTokenValue in ClientConfig must be exists, since no password is found to login.") requestAction match { case userPwd: UserPwdAction => action.addRequestPayload("userName", userPwd.getUser) action.addRequestPayload("password", userPwd.getPassword.getOrElse(pwd)) case userAction: UserAction => action.addRequestPayload("userName", userAction.getUser) action.addRequestPayload("password", pwd) case _ => if(StringUtils.isBlank(getClientConfig.getAuthTokenKey)) throw new AuthenticationFailedException("the value of authTokenKey in ClientConfig must be exists, since no user is found to login.") action.addRequestPayload("userName", getClientConfig.getAuthTokenKey) action.addRequestPayload("password", pwd) } action } override def getAuthenticationResult(response: HttpResponse, requestAction: AuthenticationAction): AuthenticationResult = { new DWSAuthenticationResult(response, requestAction.serverUrl) } }
Example 4
Source File: DWSHeartbeatResult.scala From Linkis with Apache License 2.0 | 5 votes |
package com.webank.wedatasphere.linkis.httpclient.dws.response import java.util import com.webank.wedatasphere.linkis.httpclient.discovery.HeartbeatResult import org.apache.http.HttpResponse import org.apache.http.util.EntityUtils class DWSHeartbeatResult(response: HttpResponse, serverUrl: String) extends HeartbeatResult with DWSResult { setResponse() private def setResponse(): Unit = { val entity = response.getEntity val responseBody: String = if (entity != null) { EntityUtils.toString(entity, "UTF-8") } else { null } val statusCode: Int = response.getStatusLine.getStatusCode val url: String = serverUrl val contentType: String = entity.getContentType.getValue set(responseBody, statusCode, url, contentType) } if (getStatus != 0) warn(s"heartbeat to gateway $serverUrl failed! message: $getMessage.") override val isHealthy: Boolean = getData.get("isHealthy") match { case b: java.lang.Boolean => b case s if s != null => s.toString.toBoolean case _ => false } def getGatewayList: Array[String] = getData.get("gatewayList") match { case l: util.List[String] => l.toArray(new Array[String](l.size())).map("http://" + _) case array: Array[String] => array.map("http://" + _) case _ => Array.empty } }
Example 5
Source File: AbstractAuthenticationStrategy.scala From Linkis with Apache License 2.0 | 5 votes |
package com.webank.wedatasphere.linkis.httpclient.authentication import java.util.concurrent.ConcurrentHashMap import com.webank.wedatasphere.linkis.httpclient.Client import com.webank.wedatasphere.linkis.httpclient.config.ClientConfig import com.webank.wedatasphere.linkis.httpclient.request.{Action, UserAction} import org.apache.commons.lang.StringUtils import org.apache.http.HttpResponse abstract class AbstractAuthenticationStrategy extends AuthenticationStrategy { private var client: Client = _ private val userNameToAuthentications = new ConcurrentHashMap[String, Authentication]() private var clientConfig: ClientConfig = _ protected val sessionMaxAliveTime: Long def setClient(client: Client): Unit = this.client = client def getClient: Client = client protected def getKeyByUserAndURL(user: String, serverUrl: String): String = user + "@" + serverUrl protected def getKey(requestAction: Action, serverUrl: String): String = requestAction match { case _: AuthenticationAction => null case authAction: UserAction => getKeyByUserAndURL(authAction.getUser, serverUrl) case _ if StringUtils.isNotBlank(clientConfig.getAuthTokenKey) => getKeyByUserAndURL(clientConfig.getAuthTokenKey, serverUrl) case _ => null } def setClientConfig(clientConfig: ClientConfig): Unit = this.clientConfig = clientConfig def getClientConfig: ClientConfig = clientConfig def login(requestAction: Action, serverUrl: String): Authentication = { val key = getKey(requestAction, serverUrl) if(key == null) return null if(userNameToAuthentications.containsKey(key) && !isTimeout(userNameToAuthentications.get(key))) { val authenticationAction = userNameToAuthentications.get(key) authenticationAction.updateLastAccessTime() authenticationAction } else key.intern() synchronized { var authentication = userNameToAuthentications.get(key) if(authentication == null || isTimeout(authentication)) { authentication = tryLogin(requestAction, serverUrl) userNameToAuthentications.put(key, authentication) } authentication } } def tryLogin(requestAction: Action, serverUrl: String): Authentication = { val action = getAuthenticationAction(requestAction, serverUrl) client.execute(action, 5000) match { case r: AuthenticationResult => r.getAuthentication } } protected def getAuthenticationAction(requestAction: Action, serverUrl: String): AuthenticationAction def getAuthenticationResult(response: HttpResponse, requestAction: AuthenticationAction): AuthenticationResult def isTimeout(authentication: Authentication): Boolean = System.currentTimeMillis() - authentication.getLastAccessTime >= sessionMaxAliveTime }
Example 6
Source File: HttpUtil.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.analytics.util import com.fasterxml.jackson.databind.{JsonNode, ObjectMapper} import org.apache.http.HttpResponse import org.apache.http.client.methods.{HttpGet, HttpPost} import org.apache.http.client.{ClientProtocolException, ResponseHandler} import org.apache.http.entity.{ContentType, StringEntity} import org.apache.http.impl.client.{CloseableHttpClient, HttpClients} import org.apache.http.util.EntityUtils object HttpUtil { private val mapper = new ObjectMapper() private val responseHandler = new ResponseHandler[String]() { override def handleResponse(response: HttpResponse): String = { val status = response.getStatusLine.getStatusCode if (status >= 200 && status < 300) { val entity = response.getEntity if (entity != null) EntityUtils.toString(entity) else throw new ClientProtocolException("Unexpected null response") } else { throw new ClientProtocolException("Unexpected response status: " + status) } } } def get(url: String, client: CloseableHttpClient): String = { val httpGet = new HttpGet(url) client.execute(httpGet, responseHandler) } def get(url: String): String = { val client = HttpClients.createDefault try { get(url, client) } finally { client.close() } } def getJson(url: String): JsonNode = { val client = HttpClients.createDefault try { getJson(url, client) } finally { client.close() } } def getJson(url: String, client: CloseableHttpClient): JsonNode = { mapper.readTree(get(url, client)) } def getJson(url: String, content: String, contentType: String): JsonNode = { val httpPost = new HttpPost(url) val entity = new StringEntity(content, ContentType.create(contentType, "UTF-8")) httpPost.setEntity(entity) val client = HttpClients.createDefault try { mapper.readTree(client.execute(httpPost, responseHandler)) } finally { client.close() } } }
Example 7
Source File: HttpRequestHandler.scala From sparklens with Apache License 2.0 | 5 votes |
package com.qubole.sparklens.helper import org.apache.http.client.methods.RequestBuilder import org.apache.http.entity.ContentType import org.apache.http.entity.mime.HttpMultipartMode import org.apache.http.entity.mime.MultipartEntityBuilder import org.apache.http.impl.client.HttpClients import java.io.File import org.apache.http.HttpResponse object HttpRequestHandler { def requestReport(fileName: String, email: String): HttpResponse = { val httpclient = HttpClients.createDefault() val file = new File(fileName) val builder = MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addBinaryBody("file-2", file, ContentType.DEFAULT_BINARY, file.getName()) .addTextBody("email", email) val postData = builder.build() val request = RequestBuilder .post("http://sparklens.qubole.com/generate_report/request_generate_report") .setEntity(postData) .build() val response = httpclient.execute(request) response } }
Example 8
Source File: ChuckNorris.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.plugins.chuck import java.net.URLEncoder import com.sumologic.sumobot.core.model.{IncomingMessage, OutgoingMessage, UserSender} import com.sumologic.sumobot.plugins.BotPlugin import org.apache.http.HttpResponse import org.apache.http.util.EntityUtils import play.api.libs.json.Json import slack.models.User class ChuckNorris extends BotPlugin { override protected def help: String = s"""I can Chuck Norris someone. | |chuck norris - Generally Chuck Norris. |chuck norris me - Chuck Norris yourself. |chuck norris <person> - Chuck Norris them. """.stripMargin private val ChuckNorrisAtMention = matchText(s"chuck norris $UserId.*") private val ChuckNorrisMe = matchText(s"chuck norris me") private val ChuckNorris = matchText(s"chuck norris") private val BaseUrl = "http://api.icndb.com/jokes/random" override protected def receiveIncomingMessage: ReceiveIncomingMessage = { case msg@IncomingMessage(ChuckNorris(), _, _, _, _, _, _) => msg.httpGet(BaseUrl)(convertResponse) case msg@IncomingMessage(ChuckNorrisMe(), _, _, _, _, _, sentByUser: UserSender) => msg.httpGet(url(sentByUser.slackUser))(convertResponse) case msg@IncomingMessage(ChuckNorrisAtMention(userId), _, _, _, _, _, _) => userById(userId).foreach { user => msg.httpGet(url(user))(convertResponse) } } private def convertResponse(message: IncomingMessage, response: HttpResponse): OutgoingMessage = { if (response.getStatusLine.getStatusCode == 200) { val json = Json.parse(EntityUtils.toString(response.getEntity)) val joke = json \ "value" \ "joke" val cleanJoke = joke.as[String]. replaceAllLiterally(""", "\"") message.message(cleanJoke) } else { message.message("Chuck Norris is busy right now.") } } private def url(user: User): String = user.profile match { case Some(profile) => s"$BaseUrl?firstName=${URLEncoder.encode(profile.first_name.getOrElse(user.name), "utf-8")}&lastName=${URLEncoder.encode(profile.last_name.getOrElse(""), "utf-8")}" case None => s"$BaseUrl?firstName=${URLEncoder.encode(user.name, "utf-8")}&lastName=" } }
Example 9
Source File: Advice.scala From sumobot with Apache License 2.0 | 5 votes |
package com.sumologic.sumobot.plugins.advice import com.sumologic.sumobot.core.model.{IncomingMessage, OutgoingMessage} import com.sumologic.sumobot.plugins.BotPlugin import org.apache.http.HttpResponse import org.apache.http.util.EntityUtils import play.api.libs.json.Json object Advice { val AdviceAbout = BotPlugin.matchText("(what should I do about|what do you think about|how do you handle) (.*)") val RandomAdvice = BotPlugin.matchText("I need some advice") private[advice] def parseResponse(response: HttpResponse): Seq[String] = { val json = Json.parse(EntityUtils.toString(response.getEntity)) (json \\ "advice").map(_.as[String]) } } class Advice extends BotPlugin { override protected def help: String = s""" |what should I do about <something> |what do you think about <something> |how do you handle <something> |I need some advice """.stripMargin import Advice._ override protected def receiveIncomingMessage: ReceiveIncomingMessage = { case msg@IncomingMessage(RandomAdvice(), _, _, _, _, _, _) => msg.httpGet(s"http://api.adviceslip.com/advice")(respondWithAdvice) case msg@IncomingMessage(AdviceAbout(_, something), true, _, _, _, _, _) => msg.httpGet(s"http://api.adviceslip.com/advice/search/${urlEncode(something.trim)}")(respondWithAdvice) } private def respondWithAdvice(msg: IncomingMessage, response: HttpResponse): OutgoingMessage = { if (response.getStatusLine.getStatusCode == 200) { val choices = parseResponse(response) if (choices.nonEmpty) { msg.message(chooseRandom(choices: _*)) } else { msg.response("No advice for you, sorry.") } } else { msg.response("No advice for you, sorry.") } } }