1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.bluetooth.mapclient; 18 19 /*import android.bluetooth.client.map.Bmessage; 20 import android.bluetooth.client.map.BmessageParser; 21 import android.bluetooth.client.map.Client.CharsetType; 22 import android.bluetooth.client.map.utils.ObexAppParameters; 23 */ 24 25 import android.util.Log; 26 27 import java.io.ByteArrayOutputStream; 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.io.UnsupportedEncodingException; 31 import java.nio.charset.StandardCharsets; 32 33 import javax.obex.ClientSession; 34 import javax.obex.HeaderSet; 35 import javax.obex.ResponseCodes; 36 37 final class RequestGetMessage extends Request { 38 39 private static final String TAG = "RequestGetMessage"; 40 41 private static final String TYPE = "x-bt/message"; 42 43 private Bmessage mBmessage; 44 RequestGetMessage(String handle, MasClient.CharsetType charset, boolean attachment)45 RequestGetMessage(String handle, MasClient.CharsetType charset, boolean attachment) { 46 47 mHeaderSet.setHeader(HeaderSet.NAME, handle); 48 49 mHeaderSet.setHeader(HeaderSet.TYPE, TYPE); 50 51 ObexAppParameters oap = new ObexAppParameters(); 52 53 oap.add(OAP_TAGID_CHARSET, 54 MasClient.CharsetType.UTF_8.equals(charset) ? CHARSET_UTF8 : CHARSET_NATIVE); 55 56 oap.add(OAP_TAGID_ATTACHMENT, attachment ? ATTACHMENT_ON : ATTACHMENT_OFF); 57 58 oap.addToHeaderSet(mHeaderSet); 59 } 60 61 @Override readResponse(InputStream stream)62 protected void readResponse(InputStream stream) { 63 64 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 65 byte[] buf = new byte[1024]; 66 67 try { 68 int len; 69 while ((len = stream.read(buf)) != -1) { 70 baos.write(buf, 0, len); 71 } 72 } catch (IOException e) { 73 Log.e(TAG, "I/O exception while reading response", e); 74 } 75 76 // Convert the input stream using UTF-8 since the attributes in the payload are all encoded 77 // according to it. The actual message body may need to be transcoded depending on 78 // charset/encoding defined for body-content. 79 String bmsg; 80 try { 81 bmsg = baos.toString(StandardCharsets.UTF_8.name()); 82 } catch (UnsupportedEncodingException ex) { 83 Log.e(TAG, 84 "Coudn't decode the bmessage with UTF-8. Something must be really messed up."); 85 return; 86 } 87 88 mBmessage = BmessageParser.createBmessage(bmsg); 89 90 if (mBmessage == null) { 91 mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 92 } 93 } 94 getMessage()95 public Bmessage getMessage() { 96 return mBmessage; 97 } 98 getHandle()99 public String getHandle() { 100 try { 101 return (String) mHeaderSet.getHeader(HeaderSet.NAME); 102 } catch (IOException e) { 103 Log.e(TAG, "Unexpected exception while reading handle!", e); 104 return null; 105 } 106 } 107 108 @Override execute(ClientSession session)109 public void execute(ClientSession session) throws IOException { 110 executeGet(session); 111 } 112 } 113