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 com.android.bluetooth.mapclient.MasClient.CharsetType; 20 import com.android.internal.annotations.VisibleForTesting; 21 22 import java.io.IOException; 23 import java.math.BigInteger; 24 25 import javax.obex.ClientSession; 26 import javax.obex.HeaderSet; 27 import javax.obex.ResponseCodes; 28 29 /* Place a message into current directory on MSE. */ 30 @VisibleForTesting 31 public class RequestPushMessage extends Request { 32 33 private static final String TYPE = "x-bt/message"; 34 private Bmessage mMsg; 35 private String mMsgHandle; 36 RequestPushMessage(String folder)37 private RequestPushMessage(String folder) { 38 mHeaderSet.setHeader(HeaderSet.TYPE, TYPE); 39 if (folder == null) { 40 folder = ""; 41 } 42 mHeaderSet.setHeader(HeaderSet.NAME, folder); 43 } 44 RequestPushMessage(String folder, Bmessage msg, CharsetType charset, boolean transparent, boolean retry)45 RequestPushMessage(String folder, Bmessage msg, CharsetType charset, boolean transparent, 46 boolean retry) { 47 this(folder); 48 mMsg = msg; 49 ObexAppParameters oap = new ObexAppParameters(); 50 oap.add(OAP_TAGID_TRANSPARENT, transparent ? TRANSPARENT_ON : TRANSPARENT_OFF); 51 oap.add(OAP_TAGID_RETRY, retry ? RETRY_ON : RETRY_OFF); 52 oap.add(OAP_TAGID_CHARSET, charset == CharsetType.NATIVE ? CHARSET_NATIVE : CHARSET_UTF8); 53 oap.addToHeaderSet(mHeaderSet); 54 } 55 56 @Override readResponseHeaders(HeaderSet headerset)57 protected void readResponseHeaders(HeaderSet headerset) { 58 try { 59 String handle = (String) headerset.getHeader(HeaderSet.NAME); 60 if (handle != null) { 61 /* just to validate */ 62 new BigInteger(handle, 16); 63 64 mMsgHandle = handle; 65 } 66 } catch (NumberFormatException e) { 67 mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 68 } catch (IOException e) { 69 mResponseCode = ResponseCodes.OBEX_HTTP_INTERNAL_ERROR; 70 } 71 } 72 getBMsg()73 public Bmessage getBMsg() { 74 return mMsg; 75 } 76 getMsgHandle()77 public String getMsgHandle() { 78 return mMsgHandle; 79 } 80 81 @Override execute(ClientSession session)82 public void execute(ClientSession session) throws IOException { 83 executePut(session, BmessageBuilder.createBmessage(mMsg).getBytes()); 84 } 85 } 86